home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / domacnost a kancelar / autoit / autoit-v3-setup.exe / Include / Word.au3 < prev   
Encoding:
Text File  |  2007-09-08  |  86.0 KB  |  2,062 lines

  1. #include-once
  2. #region Header
  3. #cs
  4.     Title: Microsoft Word Automation UDF Library for AutoIt3
  5.     Filename: Word.au3
  6.     Description: A collection of functions for creating, attaching to, reading from and manipulating Microsoft Word
  7.     Author: Bob Anthony
  8.     Version: V1.0-1
  9.     Last Update: 4/14/07
  10.     Requirements: AutoIt v3.2.0.1 or higher, Developed/Tested on WindowsXP Pro with Microsoft Word 2003
  11.     Notes: Errors associated with incorrect objects will be common user errors.
  12.     Special thanks to DaleHohm for letting me base this code off his IE.au3 Library!
  13.     Update History:    http://www.autoitscript.com/fileman/users/Big_Daddy/libraries/word/history.htm
  14. #ce
  15. #endregion
  16. #region Global Variables and Constants
  17. Global Const $WordAU3VersionInfo[6] = ["V", 1, 0, 1, "20070414", "V1.0-1"]
  18. Global Const $WORD_LSFW_LOCK = 1, $WORD_LSFW_UNLOCK = 2
  19. Global $__WordAU3Debug = False
  20. Global $_WordErrorNotify = True
  21. Global $oWordErrorHandler, $sWordUserErrorHandler
  22. Global _; Com Error Handler Status Strings
  23.         $WordComErrorNumber, _
  24.         $WordComErrorNumberHex, _
  25.         $WordComErrorDescription, _
  26.         $WordComErrorScriptline, _
  27.         $WordComErrorWinDescription, _
  28.         $WordComErrorSource, _
  29.         $WordComErrorHelpFile, _
  30.         $WordComErrorHelpContext, _
  31.         $WordComErrorLastDllError, _
  32.         $WordComErrorComObj, _
  33.         $WordComErrorOutput
  34. ;
  35. ; Enums
  36. ;
  37. Global Enum _; Error Status Types
  38.         $_WordStatus_Success = 0, _
  39.         $_WordStatus_GeneralError, _
  40.         $_WordStatus_ComError, _
  41.         $_WordStatus_InvalidDataType, _
  42.         $_WordStatus_InvalidObjectType, _
  43.         $_WordStatus_InvalidValue, _
  44.         $_WordStatus_ReadOnly, _
  45.         $_WordStatus_NoMatch
  46. Global Enum Step * 2 _; NotificationLevel
  47.         $_WordNotifyLevel_None = 0, _
  48.         $_WordNotifyNotifyLevel_Warning = 1, _
  49.         $_WordNotifyNotifyLevel_Error, _
  50.         $_WordNotifyNotifyLevel_ComError
  51. Global Enum Step * 2 _; NotificationMethod
  52.         $_WordNotifyMethod_Silent = 0, _
  53.         $_WordNotifyMethod_Console = 1, _
  54.         $_WordNotifyMethod_ToolTip, _
  55.         $_WordNotifyMethod_MsgBox
  56. #endregion
  57. #region Core Functions
  58. ;===============================================================================
  59. ;
  60. ; Function Name:    _WordCreate()
  61. ; Description:      Create a Microsoft Office Word Object
  62. ; Parameter(s):     $s_FilePath        - Optional: specifies the file on open upon creation (See Remarks)
  63. ;                    $f_tryAttach    - Optional: specifies whether to try to attach to an existing window
  64. ;                                        0 = (Default) do not try to attach
  65. ;                                        1 = Try to attach to an existing window
  66. ;                    $f_visible         - Optional: specifies whether the window will be visible
  67. ;                                        0 = Window is hidden
  68. ;                                        1 = (Default) Window is visible
  69. ;                    $f_takeFocus    - Optional: specifies whether to bring the attached window to focus
  70. ;                                        0 =  Do Not Bring window into focus
  71. ;                                        1 = (Default) bring window into focus
  72. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  73. ; Return Value(s):  On Success    - Returns an object variable pointing to a Word.Application object
  74. ;                   On Failure    - Returns 0 and sets @ERROR
  75. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  76. ;                                - 1 ($_WordStatus_GeneralError) = General Error
  77. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  78. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  79. ;                    @Extended    - Set to true (1) or false (0) depending on the success of $f_tryAttach
  80. ; Remark(s):        File will be created if it does not exist.
  81. ; Author(s):        Bob Anthony (Code based off IE.au3)
  82. ;
  83. ;===============================================================================
  84. ;
  85. Func _WordCreate($s_FilePath = "", $f_tryAttach = 0, $f_visible = 1, $f_takeFocus = 1)
  86.     Local $o_Result, $o_object, $o_win, $h_hwnd, $result, $f_mustUnlock = 0, $i_ErrorStatusCode = $_WordStatus_Success
  87.     
  88.     If Not $f_visible Then $f_takeFocus = 0 ; Force takeFocus to 0 for hidden window
  89.     If $s_FilePath = "" Then $f_tryAttach = 0 ; There is currently no way of attaching to a blank document
  90.     
  91.     If $f_tryAttach Then
  92.         $o_Result = _WordAttach($s_FilePath)
  93.         If IsObj($o_Result) Then
  94.             If $f_takeFocus Then
  95.                 $o_win = $o_Result.ActiveWindow
  96.                 $h_hwnd = __WordGetHWND($o_win)
  97.                 If IsHWnd($h_hwnd) Then WinActivate($h_hwnd)
  98.             EndIf
  99.             SetError($_WordStatus_Success)
  100.             SetExtended(1)
  101.             Return $o_Result
  102.         EndIf
  103.     EndIf
  104.     
  105.     If Not $f_visible Then
  106.         $result = __WordLockSetForegroundWindow($WORD_LSFW_LOCK)
  107.         If $result Then $f_mustUnlock = 1
  108.     EndIf
  109.  
  110.     ; Setup internal error handler to Trap COM errors, turn off error notification
  111.     Local $status = __WordInternalErrorHandlerRegister()
  112.     If Not $status Then __WordErrorNotify("Warning", "_WordCreate", _
  113.             "Cannot register internal error handler, cannot trap COM errors", _
  114.             "Use _WordErrorHandlerRegister() to register a user error handler")
  115.     Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
  116.     _WordErrorNotify(False)
  117.     
  118.     $o_object = ObjGet("", "Word.Application")
  119.     If Not IsObj($o_object) Or @error = $_WordStatus_ComError Then
  120.         $i_ErrorStatusCode = $_WordStatus_NoMatch
  121.     EndIf
  122.     
  123.     ; restore error notify and error handler status
  124.     _WordErrorNotify($f_NotifyStatus) ; restore notification status
  125.     __WordInternalErrorHandlerDeRegister()
  126.     
  127.     If Not $i_ErrorStatusCode = $_WordStatus_Success Then
  128.         $o_object = ObjCreate("Word.Application")
  129.         If Not IsObj($o_object) Then
  130.             __WordErrorNotify("Error", "_WordCreate", "", "Word Object Creation Failed")
  131.             SetError($_WordStatus_GeneralError)
  132.             Return 0
  133.         EndIf
  134.     EndIf
  135.  
  136.     $o_object.visible = $f_visible
  137.     
  138.     If $f_mustUnlock Then
  139.         $result = __WordLockSetForegroundWindow($WORD_LSFW_UNLOCK)
  140.         If Not $result Then __WordErrorNotify("Warning", "_WordCreate", "", "Foreground Window Unlock Failed!")
  141.         ; If the unlock doesn't work we will have created an unwanted modal window
  142.     EndIf
  143.     
  144.     If $s_FilePath = "" Then
  145.         _WordDocAdd($o_object)
  146.     Else
  147.         _WordDocOpen($o_object, $s_FilePath)
  148.     EndIf
  149.     SetError(@error)
  150.     Return $o_object
  151. EndFunc   ;==>_WordCreate
  152.  
  153. ;===============================================================================
  154. ;
  155. ; Function Name:    _WordAttach()
  156. ; Description:        Attach to the first existing instance of Microsoft Word where the
  157. ;                    search string matches based on the selected mode.
  158. ; Parameter(s):     $s_string    - String to search for
  159. ;                    $s_mode        - Optional: specifies search mode
  160. ;                                    FileName    = Name of the open document
  161. ;                                    FilePath    = (Default) Full path to the open document
  162. ;                                    HWND         = hwnd of the word window
  163. ;                                    Text         = Text from the body of the document
  164. ;                                    Title        = Title of the word window
  165. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  166. ;                    On Success    - Returns an object variable pointing to the Word.Application object
  167. ;                   On Failure    - Returns 0 and sets @ERROR
  168. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  169. ;                                - 1 ($_WordStatus_GeneralError) = General Error
  170. ;                                - 5 ($_WordStatus_InvalidValue) = Invalid Value
  171. ;                                - 7 ($_WordStatus_NoMatch) = No Match
  172. ;                    @Extended    - Contains invalid parameter number
  173. ; Author(s):        Bob Anthony (Code based off IE.au3)
  174. ;
  175. ;===============================================================================
  176. ;
  177. Func _WordAttach($s_string, $s_mode = "FilePath")
  178.     $s_mode = StringLower($s_mode)
  179.     
  180.     Local $o_Result, $o_window, $o_windows, $h_hwnd, $return, _
  181.             $i_Extended, $s_ErrorMSG = "", $i_ErrorStatusCode = $_WordStatus_Success
  182.     
  183.     ; Setup internal error handler to Trap COM errors, turn off error notification
  184.     Local $status = __WordInternalErrorHandlerRegister()
  185.     If Not $status Then __WordErrorNotify("Warning", "_WordAttach", _
  186.             "Cannot register internal error handler, cannot trap COM errors", _
  187.             "Use _WordErrorHandlerRegister() to register a user error handler")
  188.     Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
  189.     _WordErrorNotify(False)
  190.     
  191.     $o_Result = ObjGet("", "Word.Application")
  192.     If @error = $_WordStatus_ComError And $WordComErrorNumber = -2147221021 And $WordComErrorWinDescription = "Operation unavailable" Then
  193.         $i_ErrorStatusCode = $_WordStatus_NoMatch
  194.     EndIf
  195.     
  196.     If $i_ErrorStatusCode = $_WordStatus_Success Then
  197.         $o_windows = $o_Result.Application.Windows
  198.         If Not IsObj($o_windows) Then
  199.             $i_ErrorStatusCode = $_WordStatus_NoMatch
  200.         EndIf
  201.     EndIf
  202.     
  203.     If $i_ErrorStatusCode = $_WordStatus_Success Then
  204.         For $o_window In $o_windows
  205.             
  206.             Switch $s_mode
  207.                 Case "filename"
  208.                     If $o_window.Document.Name = $s_string Then
  209.                         $i_ErrorStatusCode = $_WordStatus_Success
  210.                         $o_window.Activate
  211.                         $return = $o_window.Application
  212.                     EndIf
  213.                 Case "filepath"
  214.                     If $o_window.Document.FullName = $s_string Then
  215.                         $i_ErrorStatusCode = $_WordStatus_Success
  216.                         $o_window.Activate
  217.                         $return = $o_window.Application
  218.                     EndIf
  219.                 Case "hwnd"
  220.                     $h_hwnd = __WordGetHWND($o_window)
  221.                     If IsHWnd($h_hwnd) Then
  222.                         If $h_hwnd = $s_string Then
  223.                             $i_ErrorStatusCode = $_WordStatus_Success
  224.                             $o_window.Activate
  225.                             $return = $o_window.Application
  226.                         EndIf
  227.                     EndIf
  228.                 Case "text"
  229.                     If StringInStr($o_window.Document.Range.Text, $s_string) Then
  230.                         $i_ErrorStatusCode = $_WordStatus_Success
  231.                         $o_window.Activate
  232.                         $return = $o_window.Application
  233.                     EndIf
  234.                 Case "title"
  235.                     If ($o_window.Caption & " - " & $o_window.Application.Caption) = $s_string Then
  236.                         $i_ErrorStatusCode = $_WordStatus_Success
  237.                         $o_window.Activate
  238.                         $return = $o_window.Application
  239.                     EndIf
  240.                 Case Else
  241.                     ; Invalid Mode
  242.                     $i_ErrorStatusCode = $_WordStatus_InvalidValue
  243.                     $s_ErrorMSG = "Invalid Mode Specified"
  244.                     $i_Extended = 2
  245.             EndSwitch
  246.         Next
  247.         If Not IsObj($return) Then
  248.             $i_ErrorStatusCode = $_WordStatus_NoMatch
  249.         EndIf
  250.     EndIf
  251.     
  252.     ; restore error notify and error handler status
  253.     _WordErrorNotify($f_NotifyStatus) ; restore notification status
  254.     __WordInternalErrorHandlerDeRegister()
  255.     
  256.     Switch $i_ErrorStatusCode
  257.         Case $_WordStatus_Success
  258.             SetError($_WordStatus_Success)
  259.             Return $return
  260.         Case $_WordStatus_NoMatch
  261.             __WordErrorNotify("Warning", "_WordAttach", "$_WordStatus_NoMatch")
  262.             SetError($_WordStatus_NoMatch)
  263.             Return 0
  264.         Case $_WordStatus_InvalidValue
  265.             __WordErrorNotify("Error", "_WordAttach", "$_WordStatus_InvalidValue", $s_ErrorMSG)
  266.             SetError($_WordStatus_InvalidValue, $i_Extended)
  267.             Return 0
  268.         Case Else
  269.             __WordErrorNotify("Error", "_WordAttach", "$_WordStatus_GeneralError", "Invalid Error Status - Notify Word.au3 developer")
  270.             SetError($_WordStatus_GeneralError)
  271.             Return 0
  272.     EndSwitch
  273. EndFunc   ;==>_WordAttach
  274.  
  275. ;===============================================================================
  276. ;
  277. ; Function Name:    _WordQuit()
  278. ; Description:      Close the window and remove the object reference to it
  279. ; Parameter(s):     $o_object            - Object variable of a Word.Application
  280. ;                    $i_SaveChanges        - Optional: specifies the save action for the document
  281. ;                                             0 = Do not save changes
  282. ;                                            -1 = Save changes
  283. ;                                            -2 = (Default) Prompt to save changes
  284. ;                    $i_OriginalFormat    - Optional: specifies the save format for the document
  285. ;                                            0 = Word Document
  286. ;                                            1 = (Default) Original Document Format
  287. ;                                            2 = Prompt User
  288. ;                    $f_RouteDocument    - Optional: specifies whether to route the document to the next recipient
  289. ;                                            0 = (Default) do not route
  290. ;                                            1 = route to next recipient
  291. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  292. ; Return Value(s):  On Success    - Returns 1
  293. ;                   On Failure    - Returns 0 and sets @ERROR
  294. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  295. ;                                - 1 ($_WordStatus_GeneralError) = General Error
  296. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  297. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  298. ;                    @Extended    - Contains invalid parameter number
  299. ; Author(s):        Bob Anthony (Code based off IE.au3)
  300. ;
  301. ;===============================================================================
  302. ;
  303. Func _WordQuit(ByRef $o_object, $i_SaveChanges = -2, $i_OriginalFormat = 1, $f_RouteDocument = 0)
  304.     If Not IsObj($o_object) Then
  305.         __WordErrorNotify("Error", "_WordQuit", "$_WordStatus_InvalidDataType")
  306.         SetError($_WordStatus_InvalidDataType, 1)
  307.         Return 0
  308.     EndIf
  309.     ;
  310.     If Not __WordIsObjType($o_object, "application") Then
  311.         __WordErrorNotify("Error", "_WordQuit", "$_WordStatus_InvalidObjectType")
  312.         SetError($_WordStatus_InvalidObjectType, 1)
  313.         Return 0
  314.     EndIf
  315.     
  316.     $o_object.Quit ($i_SaveChanges, $i_OriginalFormat, $f_RouteDocument)
  317.     $o_object = 0
  318.     SetError($_WordStatus_Success)
  319.     Return 1
  320. EndFunc   ;==>_WordQuit
  321. #endregion
  322. #region Document Functions
  323. ;===============================================================================
  324. ;
  325. ; Function Name:    _WordDocAdd()
  326. ; Description:      Returns an object variable representing a new empty document
  327. ; Parameter(s):     $o_object        - Object variable of a Word.Application object
  328. ;                    $i_DocumentType    - Optional: specifies the new document type
  329. ;                                        0 = (Default) blank document
  330. ;                                        1 = Web page
  331. ;                                        2 = Email Message (need to figure out why this doesn't open)
  332. ;                                        3 = Frameset
  333. ;                                        4 = XML Document
  334. ;                    $s_Template        - Optional: specifies name of the template to be used for the new document
  335. ;                                        "" = (Default) normal template is used
  336. ;                    $f_NewTemplate    - Optional: specifies whether to open the document as a template
  337. ;                                        0 = (Default) do not open as template
  338. ;                                        1 = open as template
  339. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  340. ; Return Value(s):  On Success    - Returns an object variable pointing to a Word.Application, document object
  341. ;                   On Failure    - Returns 0 and sets @ERROR
  342. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  343. ;                                - 1 ($_WordStatus_GeneralError) = General Error
  344. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  345. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  346. ;                    @Extended    - Contains invalid parameter number
  347. ; Author(s):        Bob Anthony (Code based off IE.au3)
  348. ;
  349. ;===============================================================================
  350. ;
  351. Func _WordDocAdd(ByRef $o_object, $i_DocumentType = 0, $s_Template = "", $f_NewTemplate = 0)
  352.     If Not IsObj($o_object) Then
  353.         __WordErrorNotify("Error", "_WordDocAdd", "$_WordStatus_InvalidDataType")
  354.         SetError($_WordStatus_InvalidDataType, 1)
  355.         Return 0
  356.     EndIf
  357.     ;
  358.     If Not __WordIsObjType($o_object, "application") Then
  359.         __WordErrorNotify("Error", "_WordDocAdd", "$_WordStatus_InvalidObjectType")
  360.         SetError($_WordStatus_InvalidObjectType, 1)
  361.         Return 0
  362.     EndIf
  363.     ;
  364.     Local $o_doc
  365.     
  366.     $o_doc = $o_object.Documents.Add ($s_Template, $f_NewTemplate, $i_DocumentType)
  367.     If Not IsObj($o_doc) Then
  368.         __WordErrorNotify("Error", "_WordDocAdd", "", "Document Object Creation Failed")
  369.         SetError($_WordStatus_GeneralError)
  370.         Return 0
  371.     EndIf
  372.     
  373.     SetError($_WordStatus_Success)
  374.     Return $o_doc
  375. EndFunc   ;==>_WordDocAdd
  376.  
  377. ;===============================================================================
  378. ;
  379. ; Function Name:    _WordDocOpen()
  380. ; Description:      Opens a Microsoft Word Document
  381. ; Parameter(s):     $o_object                - Object variable of a Word.Application object
  382. ;                    $s_FilePath                - Full path of the document to open (See Remarks)
  383. ;                    $f_ConfirmConversions    - Optional: Specifies whether to display the Convert File dialog box
  384. ;                                                if the file isn't in Microsoft Word format.
  385. ;                                                0 = (Default) Do not display
  386. ;                                                1 = Display
  387. ;                    $i_Format                - Optional: The file converter to be used to open the document.
  388. ;                                                0 = (Default) The existing format
  389. ;                                                1 = Microsoft Word Document format
  390. ;                                                2 = Microsoft Word Template format
  391. ;                                                3 = Rich text format (RTF)
  392. ;                                                4 = Unencoded text format
  393. ;                                                5 = Unicode text format or Encoded text format
  394. ;                                                6 = Microsoft Word format that is backward compatible with earlier versions of Microsoft Word
  395. ;                                                7 = HTML format
  396. ;                                                8 = XML format
  397. ;                    $f_ReadOnly                - Optional: Specifies whether to open the document as read-only.
  398. ;                                                Note: This argument doesn't override the read-only recommended setting on a saved document.
  399. ;                                                0 = (Default) Open as Read/Write
  400. ;                                                1 = Open as Read Only
  401. ;                    $f_Revert                - Optional: Controls what happens if $s_FilePath is an open document.
  402. ;                                                0 = (Default) Activate the open document
  403. ;                                                1 = Discard any unsaved changes to the open document and reopen the file
  404. ;                    $f_AddToRecentFiles        - Optional: Specifies whether to add the file name to the list of recently used
  405. ;                                                files at the bottom of the File menu.
  406. ;                                                0 = (Default) Do not add
  407. ;                                                1 = Add
  408. ;                    $s_PasswordDocument        - Optional: The password for opening the document.
  409. ;                                                "" = (Default) Null
  410. ;                    $s_WritePasswordDocument- Optional: The password for saving changes to the document.
  411. ;                                                "" = (Default) Null
  412. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  413. ; Return Value(s):  On Success    - Returns an object variable pointing to a Word.Application, document object
  414. ;                   On Failure    - Returns 0 and sets @ERROR
  415. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  416. ;                                - 1 ($_WordStatus_GeneralError) = General Error
  417. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  418. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  419. ;                    @Extended    - Contains invalid parameter number
  420. ; Remark(s):        File will be created if it does not exist.
  421. ; Author(s):        Bob Anthony (Code based off IE.au3)
  422. ;
  423. ;===============================================================================
  424. ;
  425. Func _WordDocOpen(ByRef $o_object, $s_FilePath, $f_ConfirmConversions = 0, $i_Format = 0, $f_ReadOnly = 0, $f_Revert = 0, $f_AddToRecentFiles = 0, $s_PasswordDocument = "", $s_WritePasswordDocument = "")
  426.     If Not IsObj($o_object) Then
  427.         __WordErrorNotify("Error", "_WordDocOpen", "$_WordStatus_InvalidDataType")
  428.         SetError($_WordStatus_InvalidDataType, 1)
  429.         Return 0
  430.     EndIf
  431.     ;
  432.     If Not __WordIsObjType($o_object, "application") Then
  433.         __WordErrorNotify("Error", "_WordDocOpen", "$_WordStatus_InvalidObjectType")
  434.         SetError($_WordStatus_InvalidObjectType, 1)
  435.         Return 0
  436.     EndIf
  437.     ;
  438.     Local $o_doc
  439.     
  440.     If Not FileExists($s_FilePath) Then
  441.         __WordErrorNotify("Warning", "_WordDocOpen", "", "The specified file does not exist, but we will attempt to create it.")
  442.         $o_doc = _WordDocAdd($o_object)
  443.         If @error Then
  444.             SetError(@error)
  445.             Return $o_doc
  446.         EndIf
  447.         _WordDocSaveAs($o_doc, $s_FilePath)
  448.         If @error Then
  449.             __WordErrorNotify("Error", "_WordDocOpen", "", "The specified file could not be created.")
  450.             SetError($_WordStatus_GeneralError, 2)
  451.             Return 0
  452.         Else
  453.             __WordErrorNotify("Info", "_WordDocOpen", "", "The specified file was created successfully.")
  454.             SetError($_WordStatus_Success)
  455.             Return $o_doc
  456.         EndIf
  457.     EndIf
  458.     
  459.     $o_doc = $o_object.Documents.Open ($s_FilePath, $f_ConfirmConversions, $f_ReadOnly, $f_AddToRecentFiles, _
  460.             $s_PasswordDocument, "", $f_Revert, $s_WritePasswordDocument, "", $i_Format)
  461.     If Not IsObj($o_doc) Then
  462.         __WordErrorNotify("Error", "_WordDocOpen", "", "Document Object Creation Failed")
  463.         SetError($_WordStatus_GeneralError)
  464.         Return 0
  465.     EndIf
  466.     
  467.     SetError($_WordStatus_Success)
  468.     Return $o_doc
  469. EndFunc   ;==>_WordDocOpen
  470.  
  471. ;===============================================================================
  472. ;
  473. ; Function Name:    _WordDocSave()
  474. ; Description:      Saves a previously opened document
  475. ; Parameter(s):     $o_object            - Object variable of a Word.Application, document object
  476. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  477. ; Return Value(s):  On Success    - Returns 1
  478. ;                   On Failure    - Returns 0 and sets @ERROR
  479. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  480. ;                                - 1 ($_WordStatus_GeneralError) = General Error
  481. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  482. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  483. ;                    @Extended    - Contains invalid parameter number
  484. ; Remark(s):        If a document hasn't been saved before, the Save As dialog box prompts the user for a file name
  485. ; Author(s):        Bob Anthony (Code based off IE.au3)
  486. ;
  487. ;===============================================================================
  488. ;
  489. Func _WordDocSave(ByRef $o_object)
  490.     If Not IsObj($o_object) Then
  491.         __WordErrorNotify("Error", "_WordDocSave", "$_WordStatus_InvalidDataType")
  492.         SetError($_WordStatus_InvalidDataType, 1)
  493.         Return 0
  494.     EndIf
  495.     ;
  496.     If Not __WordIsObjType($o_object, "document") Then
  497.         __WordErrorNotify("Error", "_WordDocSave", "$_WordStatus_InvalidObjectType")
  498.         SetError($_WordStatus_InvalidObjectType, 1)
  499.         Return 0
  500.     EndIf
  501.     
  502.     If Not FileExists($o_object.FullName) Then
  503.         __WordErrorNotify("Error", "_WordDocSave", "", "The specified document has not be saved, please use _WordDocSaveAs first.")
  504.         SetError($_WordStatus_GeneralError, 1)
  505.         Return 0
  506.     EndIf
  507.     
  508.     $o_object.Save
  509.     SetError($_WordStatus_Success)
  510.     Return 1
  511. EndFunc   ;==>_WordDocSave
  512.  
  513. ;===============================================================================
  514. ;
  515. ; Function Name:    _WordDocSaveAs()
  516. ; Description:      Saves the specified document with a new name or format.
  517. ; Parameter(s):     $o_object                - Object variable of a Word.Application, document object
  518. ;                    $s_FilePath                - Optional: The full file path for saving the document. (See Remarks)
  519. ;                                                "" = (Default) If the document has never been saved,
  520. ;                                                    the default name is used (for example, Document1.doc)
  521. ;                    $i_Format                - Optional: The format in which the document is saved.
  522. ;                                                0 = (Default) Microsoft Word format
  523. ;                                                1 = Microsoft Word template format
  524. ;                                                2 = Microsoft Windows text format
  525. ;                                                3 = Microsoft Windows text format with line breaks preserved
  526. ;                                                4 = Microsoft DOS text format
  527. ;                                                5 = Microsoft DOS text with line breaks preserved
  528. ;                                                6 = Rich text format (RTF)
  529. ;                                                7 = Unicode text format or Encoded text format
  530. ;                                                8 = Standard HTML format
  531. ;                                                9 = Web archive format
  532. ;                                                10 = Filtered HTML format
  533. ;                                                11 = Extensible Markup Language (XML) format
  534. ;                    $f_ReadOnlyRecommended    - Optional: Specifies whether to have Microsoft Word suggest
  535. ;                                                read-only status whenever the document is opened.
  536. ;                                                0 = (Default) Do not suggest read only
  537. ;                                                1 = Suggest read only
  538. ;                    $f_AddToRecentFiles        - Optional: Specifies whether to add the file name to the list of recently used
  539. ;                                                files at the bottom of the File menu.
  540. ;                                                0 = (Default) Do not add
  541. ;                                                1 = Add
  542. ;                    $f_LockComments            - Optional: Specifies whether to lock the document for comments.
  543. ;                                                0 = (Default) Do not lock for comments
  544. ;                                                1 = Lock for comments
  545. ;                    $s_Password                - Optional: A password string for opening the document. (See Remarks)
  546. ;                    $s_WritePassword        - Optional: A password string for saving changes to the document. (See Remarks)
  547. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  548. ; Return Value(s):  On Success    - Returns 1
  549. ;                   On Failure    - Returns 0 and sets @ERROR
  550. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  551. ;                                - 1 ($_WordStatus_GeneralError) = General Error
  552. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  553. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  554. ;                    @Extended    - Contains invalid parameter number
  555. ; Remark(s):        If a document with the specified file name already exists, the document is overwritten without the user being prompted first.
  556. ;                    Avoid using hard-coded passwords in your applications. If a password is required in a procedure,
  557. ;                    request the password from the user, store it in a variable, and then use the variable in your code.
  558. ; Author(s):        Bob Anthony (Code based off IE.au3)
  559. ;
  560. ;===============================================================================
  561. ;
  562. Func _WordDocSaveAs(ByRef $o_object, $s_FilePath = "", $i_Format = 0, $f_ReadOnlyRecommended = 0, $f_AddToRecentFiles = 0, $f_LockComments = 0, $s_Password = "", $s_WritePassword = "")
  563.     If Not IsObj($o_object) Then
  564.         __WordErrorNotify("Error", "_WordDocSaveAs", "$_WordStatus_InvalidDataType")
  565.         SetError($_WordStatus_InvalidDataType, 1)
  566.         Return 0
  567.     EndIf
  568.     ;
  569.     If Not __WordIsObjType($o_object, "document") Then
  570.         __WordErrorNotify("Error", "_WordDocSaveAs", "$_WordStatus_InvalidObjectType")
  571.         SetError($_WordStatus_InvalidObjectType, 1)
  572.         Return 0
  573.     EndIf
  574.     
  575.     If FileExists($s_FilePath) Then
  576.         __WordErrorNotify("Warning", "_WordDocSaveAs", "", "The specified file path already exists and will be overwritten.")
  577.     EndIf
  578.     
  579.     If $s_FilePath = "" Then
  580.         $s_FilePath = $o_object.FullName
  581.     EndIf
  582.     
  583.     $o_object.SaveAs ($s_FilePath, $i_Format, $f_LockComments, $s_Password, _
  584.             $f_AddToRecentFiles, $s_WritePassword, $f_ReadOnlyRecommended)
  585.     SetError($_WordStatus_Success)
  586.     Return 1
  587. EndFunc   ;==>_WordDocSaveAs
  588.  
  589. ;===============================================================================
  590. ;
  591. ; Function Name:    _WordDocClose()
  592. ; Description:      Closes a previously opened word document
  593. ; Parameter(s):     $o_object            - Object variable of a Word.Application, document object
  594. ;                    $i_SaveChanges        - Optional: specifies the save action for the document
  595. ;                                             0 = Do not save changes
  596. ;                                            -1 = Save changes
  597. ;                                            -2 = (Default) Prompt to save changes
  598. ;                    $i_OriginalFormat    - Optional: specifies the save format for the document
  599. ;                                            0 = Word Document
  600. ;                                            1 = Original Document Format
  601. ;                                            2 = (Default) Prompt User
  602. ;                    $f_RouteDocument    - Optional: specifies whether to route the document to the next recipient
  603. ;                                        0 = (Default) do not route
  604. ;                                        1 = route to next recipient
  605. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  606. ; Return Value(s):  On Success    - Returns 1
  607. ;                   On Failure    - Returns 0 and sets @ERROR
  608. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  609. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  610. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  611. ;                    @Extended    - Contains invalid parameter number
  612. ; Author(s):        Bob Anthony (Code based off IE.au3)
  613. ;
  614. ;===============================================================================
  615. ;
  616. Func _WordDocClose(ByRef $o_object, $i_SaveChanges = -2, $i_OriginalFormat = 2, $f_RouteDocument = 0)
  617.     If Not IsObj($o_object) Then
  618.         __WordErrorNotify("Error", "_WordDocClose", "$_WordStatus_InvalidDataType")
  619.         SetError($_WordStatus_InvalidDataType, 1)
  620.         Return 0
  621.     EndIf
  622.     ;
  623.     If Not __WordIsObjType($o_object, "document") Then
  624.         __WordErrorNotify("Error", "_WordDocClose", "$_WordStatus_InvalidObjectType")
  625.         SetError($_WordStatus_InvalidObjectType, 1)
  626.         Return 0
  627.     EndIf
  628.     
  629.     $o_object.Close ($i_SaveChanges, $i_OriginalFormat, $f_RouteDocument)
  630.     SetError($_WordStatus_Success)
  631.     Return 1
  632. EndFunc   ;==>_WordDocClose
  633.  
  634. ;===============================================================================
  635. ;
  636. ; Function Name:    _WordDocGetCollection()
  637. ; Description:        Returns a collection object containing all documents
  638. ; Parameter(s):        $o_object    - Object variable of a Word.Application object
  639. ;                    $v_index    - Optional: Specifies whether to return a collection or indexed instance.
  640. ;                                    -1 = (Default) Returns a collection
  641. ;                                     0 = Returns the Active Document
  642. ;                                     The document name or index number to return (1 based)
  643. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  644. ; Return Value(s):  On Success     - Returns an object collection of all documents, @EXTENDED = document count
  645. ;                   On Failure    - Returns 0 and sets @ERROR
  646. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  647. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  648. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  649. ;                                - 5 ($_WordStatus_InvalidValue) = Invalid Value
  650. ;                                - 7 ($_WordStatus_NoMatch) = No Match
  651. ;                    @Extended    - Contains invalid parameter number
  652. ; Author(s):        Bob Anthony (Code based off IE.au3)
  653. ;
  654. ;===============================================================================
  655. ;
  656. Func _WordDocGetCollection(ByRef $o_object, $v_index = -1)
  657.     If Not IsObj($o_object) Then
  658.         __WordErrorNotify("Error", "_WordDocGetCollection", "$_WordStatus_InvalidDataType")
  659.         SetError($_WordStatus_InvalidDataType, 1)
  660.         Return 0
  661.     EndIf
  662.     ;
  663.     If Not __WordIsObjType($o_object, "application") Then
  664.         __WordErrorNotify("Error", "_WordDocGetCollection", "$_WordStatus_InvalidObjectType")
  665.         SetError($_WordStatus_InvalidObjectType, 1)
  666.         Return 0
  667.     EndIf
  668.     ;
  669.     If IsNumber($v_index) Then
  670.         Select
  671.             Case $v_index = -1
  672.                 SetError($_WordStatus_Success)
  673.                 SetExtended($o_object.Documents.Count)
  674.                 Return $o_object.Documents
  675.             Case $v_index = 0
  676.                 SetError($_WordStatus_Success)
  677.                 SetExtended($o_object.Documents.Count)
  678.                 Return $o_object.ActiveDocument
  679.             Case $v_index > 0 And $v_index <= $o_object.Documents.Count
  680.                 SetError($_WordStatus_Success)
  681.                 SetExtended($o_object.Documents.Count)
  682.                 Return $o_object.Documents ($v_index)
  683.             Case $v_index < -1
  684.                 __WordErrorNotify("Error", "_WordDocGetCollection", "$_WordStatus_InvalidValue")
  685.                 SetError($_WordStatus_InvalidValue, 2)
  686.                 Return 0
  687.             Case Else
  688.                 __WordErrorNotify("Warning", "_WordDocGetCollection", "$_WordStatus_NoMatch")
  689.                 SetError($_WordStatus_NoMatch, 2)
  690.                 Return 0
  691.         EndSelect
  692.     Else
  693.         For $o_doc In $o_object.Documents
  694.             If $o_doc.Name = $v_index Then
  695.                 SetError($_WordStatus_Success)
  696.                 SetExtended($o_object.Documents.Count)
  697.                 Return $o_doc
  698.             EndIf
  699.         Next
  700.         __WordErrorNotify("Warning", "_WordDocGetCollection", "$_WordStatus_NoMatch")
  701.         SetError($_WordStatus_NoMatch, 2)
  702.         Return 0
  703.     EndIf
  704. EndFunc   ;==>_WordDocGetCollection
  705.  
  706. ;===============================================================================
  707. ;
  708. ; Function Name:    _WordDocFindReplace()
  709. ; Description:      Runs the specified find and replace operation.
  710. ; Parameter(s):     $o_object            - Object variable of a Word.Application, document object
  711. ;                    $s_FindText            - Optional: The text to be searched for. (See Remarks)
  712. ;                                            "" = (Default) Used to search for formatting only.
  713. ;                    $s_ReplaceWith        - Optional: The replacement text. (See Remarks)
  714. ;                                            "" = (Default) Delete the text specified by $s_FindText
  715. ;                    $i_Replace            - Optional: Specifies how many replacements are to be made.
  716. ;                                            0 = Replace no occurrences
  717. ;                                            1 = Replace the first occurrence encountered
  718. ;                                            2 = (Default) Replace all occurrences
  719. ;                    $v_SearchRange        - Optional: Specifies the Selection or Range to search.
  720. ;                                            -1 = Specifies the current selection
  721. ;                                            0 = (Default) Specifies the entire document
  722. ;                                            Any range object
  723. ;                    $f_MatchCase        - Optional: Specifies whether the find text will be case-sensitive.
  724. ;                                            0 = (Default) Not case-sensitive
  725. ;                                            1 = Case-sensitive
  726. ;                    $f_MatchWholeWord    - Optional: Specifies whether to have the find operation locate only entire words,
  727. ;                                            not text that's part of a larger word.
  728. ;                                            0 = (Default) Match partial words
  729. ;                                            1 = Only match entire words
  730. ;                    $f_MatchWildcards    - Optional: Specifies whether to have $s_FindText be a special search operator.
  731. ;                                            0 = (Default) Not a special search operator
  732. ;                                            1 = Special search operator
  733. ;                    $f_MatchSoundsLike    - Optional: Specifies whether to have the find operation locate words that sound similar to $s_FindText.
  734. ;                                            0 = (Default) Do not find similar sounding words
  735. ;                                            1 = Find similar sounding words
  736. ;                    $f_MatchAllWordForms- Optional: Specifies whether to have the find operation locate all forms of the find text
  737. ;                                            (for example, "sit" locates "sitting" and "sat").
  738. ;                                            0 = (Default) Do not match other word forms
  739. ;                                            1 = Match all word forms
  740. ;                    $f_Forward            - Optional: Specifies which direction to search.
  741. ;                                            0 = Search backward (toward the start of the document)
  742. ;                                            1 = (Default) Search forward (toward the end of the document)
  743. ;                    $i_Wrap                - Optional: Controls what happens if the search begins at a point other than the beginning of the document
  744. ;                                            and the end of the document is reached (or vice versa if $f_Forward is set to 0).
  745. ;                                            0 = The find operation ends if the beginning or end of the search range is reached
  746. ;                                            1 = (Default) The find operation continues if the beginning or end of the search range is reached
  747. ;                    $f_Format            - Optional: Specifies whether to have the find operation locate formatting in addition to or instead of the find text.
  748. ;                                            0 = (Default) Do not locate formatting
  749. ;                                            1 = Locate formatting
  750. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  751. ; Return Value(s):  On Success    - Returns 1
  752. ;                   On Failure    - Returns 0 and sets @ERROR
  753. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  754. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  755. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  756. ;                                - 5 ($_WordStatus_InvalidValue) = Invalid Value
  757. ;                                - 7 ($_WordStatus_NoMatch) = No Match
  758. ;                    @Extended    - Contains invalid parameter number
  759. ; Author(s):        Bob Anthony (Code based off IE.au3)
  760. ;
  761. ;===============================================================================
  762. ;
  763. Func _WordDocFindReplace(ByRef $o_object, $s_FindText = "", $s_ReplaceWith = "", $i_Replace = 2, $v_SearchRange = 0, $f_MatchCase = 0, $f_MatchWholeWord = 0, $f_MatchWildcards = 0, $f_MatchSoundsLike = 0, $f_MatchAllWordForms = 0, $f_Forward = 1, $i_Wrap = 1, $f_Format = 0)
  764.     If Not IsObj($o_object) Then
  765.         __WordErrorNotify("Error", "_WordDocFindReplace", "$_WordStatus_InvalidDataType")
  766.         SetError($_WordStatus_InvalidDataType, 1)
  767.         Return 0
  768.     EndIf
  769.     ;
  770.     If Not __WordIsObjType($o_object, "document") Then
  771.         __WordErrorNotify("Error", "_WordDocFindReplace", "$_WordStatus_InvalidObjectType")
  772.         SetError($_WordStatus_InvalidObjectType, 1)
  773.         Return 0
  774.     EndIf
  775.     ;
  776.     Local $o_Find, $return
  777.     
  778.     Select
  779.         Case $v_SearchRange = -1
  780.             $v_SearchRange = $o_object.Application.Selection.Range
  781.         Case $v_SearchRange = 0
  782.             $v_SearchRange = $o_object.Range
  783.         Case $v_SearchRange > -1
  784.             __WordErrorNotify("Error", "_WordDocFindReplace", "$_WordStatus_InvalidValue")
  785.             SetError($_WordStatus_InvalidValue, 5)
  786.             Return 0
  787.         Case Else
  788.             If Not __WordIsObjType($v_SearchRange, "range") Then
  789.                 __WordErrorNotify("Error", "_WordDocFindReplace", "$_WordStatus_InvalidObjectType")
  790.                 SetError($_WordStatus_InvalidObjectType, 5)
  791.                 Return 0
  792.             EndIf
  793.     EndSelect
  794.     
  795.     $o_Find = $v_SearchRange.Find
  796.     With $o_Find
  797.         .ClearFormatting ()
  798.         .Replacement.ClearFormatting ()
  799.         $return = .Execute($s_FindText, $f_MatchCase, $f_MatchWholeWord, $f_MatchWildcards, $f_MatchSoundsLike, _
  800.                 $f_MatchAllWordForms, $f_Forward, $i_Wrap, $f_Format, $s_ReplaceWith, $i_Replace)
  801.     EndWith
  802.     
  803.     If $return Then
  804.         SetError($_WordStatus_Success)
  805.         Return 1
  806.     Else
  807.         __WordErrorNotify("Warning", "_WordDocFindReplace", "$_WordStatus_NoMatch")
  808.         SetError($_WordStatus_NoMatch)
  809.         Return 0
  810.     EndIf
  811. EndFunc   ;==>_WordDocFindReplace
  812.  
  813. ;===============================================================================
  814. ;
  815. ; Function Name:    _WordDocPrint()
  816. ; Description:      Prints all or part of the specified document.
  817. ; Parameter(s):     $o_object        - Object variable of a Word.Application, document object
  818. ;                    $f_Background    - Optional: Specifies whether to have the script continue while
  819. ;                                        Microsoft Word prints the document. (See Remarks)
  820. ;                                        0 = (Default) Wait for document to print
  821. ;                                        1 = Continue script without waiting
  822. ;                    $i_Copies        - Optional: The number of copies to be printed.
  823. ;                    $i_Orientation    - Optional: Sets the orientation of the page.
  824. ;                                        -1 = (Default) Current document orientation
  825. ;                                         0 = Portrait
  826. ;                                         1 = Landscape
  827. ;                    $f_Collate        - Optional: Specifies whether to print all pages of the document before printing the next copy.
  828. ;                                        0 = Do not collate
  829. ;                                        1 = (Default) Collate
  830. ;                    $s_Printer        - Optional: Sets the name of the printer.
  831. ;                    $i_Range        - Optional: Specifies the page range.
  832. ;                                        0 = (Default) The entire document
  833. ;                                        1 = The current selection
  834. ;                                        2 = The current page
  835. ;                                        3 = A specified range (must specify $i_From and $i_To)
  836. ;                                        4 = A specified range of pages (must specify $s_Pages)
  837. ;                    $i_From            - Optional: The starting page number when $i_Range is set to 3.
  838. ;                    $i_To            - Optional: The ending page number when $i_Range is set to 3.
  839. ;                    $s_Pages        - Optional: The page numbers and page ranges to be printed, separated by commas,
  840. ;                                        when $i_Range is set to 4. For example, "2, 6-10" prints page 2 and pages 6 through 10.
  841. ;                    $i_PageType        - Optional: The type of pages to be printed.
  842. ;                                        0 = (Default) All pages
  843. ;                                        1 = Odd-numbered pages only
  844. ;                                        2 = Even-numbered pages only
  845. ;                    $i_Item            - Optional: The item to be printed.
  846. ;                                        0 = (Default) Current document content
  847. ;                                        1 = Properties in the current document
  848. ;                                        2 = Comments and Markup in the current document
  849. ;                                        3 = Styles in the current document
  850. ;                                        4 = Autotext entries in the current document
  851. ;                                        5 = Key assignments in the current document
  852. ;                                        6 = An envelope
  853. ;                                        7 = Current document content including markup
  854. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  855. ; Return Value(s):  On Success    - Returns 1
  856. ;                   On Failure    - Returns 0 and sets @ERROR
  857. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  858. ;                                - 1 ($_WordStatus_GeneralError) = General Error
  859. ;                                - 2 ($_WordStatus_ComError) = Com Error
  860. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  861. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  862. ;                                - 5 ($_WordStatus_InvalidValue) = Invalid Value
  863. ;                    @Extended    - Contains invalid parameter number
  864. ; Remark(s):        Specifying $f_Background does NOT pause the script until the document is finished printing,
  865. ;                    it only pauses until Microsoft Word finishes sending the document to the printer.
  866. ; Author(s):        Bob Anthony (Code based off IE.au3)
  867. ;
  868. ;===============================================================================
  869. ;
  870. Func _WordDocPrint(ByRef $o_object, $f_Background = 0, $i_Copies = 1, $i_Orientation = -1, $f_Collate = 1, $s_Printer = "", $i_Range = 0, $i_From = "", $i_To = "", $s_Pages = "", $i_PageType = 0, $i_Item = 0)
  871.     If Not IsObj($o_object) Then
  872.         __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidDataType")
  873.         SetError($_WordStatus_InvalidDataType, 1)
  874.         Return 0
  875.     EndIf
  876.     ;
  877.     If Not __WordIsObjType($o_object, "document") Then
  878.         __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidObjectType")
  879.         SetError($_WordStatus_InvalidObjectType, 1)
  880.         Return 0
  881.     EndIf
  882.     ;
  883.     Local $s_ActivePrinter, $i_Extended, $i_DocOrientation = "", $i_ErrorStatusCode = $_WordStatus_Success, $s_ErrorMSG = ""
  884.     
  885.     Switch $i_Range
  886.         Case 3
  887.             If Not $i_From Or Not $i_To Then
  888.                 __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", _
  889.                         "When $i_Range is set to 3, then you must specify $i_From and $i_To.")
  890.                 SetError($_WordStatus_InvalidValue, 7)
  891.                 Return 0
  892.             EndIf
  893.         Case 4
  894.             If Not $s_Pages Then
  895.                 __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", _
  896.                         "When $i_Range is set to 4, you must specify $s_Pages.")
  897.                 SetError($_WordStatus_InvalidValue, 7)
  898.                 Return 0
  899.             EndIf
  900.     EndSwitch
  901.     
  902.     $i_Orientation = String($i_Orientation)
  903.     If $i_Orientation <> "-1" Then
  904.         Switch $i_Orientation
  905.             Case "0", "1"
  906.                 $i_DocOrientation = String($o_object.PageSetup.Orientation)
  907.                 If $i_DocOrientation <> $i_Orientation Then
  908.                     $o_object.PageSetup.Orientation = $i_Orientation
  909.                 EndIf
  910.             Case Else
  911.                 __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue")
  912.                 SetError($_WordStatus_InvalidValue, 4)
  913.                 Return 0
  914.         EndSwitch
  915.     EndIf
  916.     
  917.     ; Setup internal error handler to Trap COM errors, turn off error notification
  918.     Local $status = __WordInternalErrorHandlerRegister()
  919.     If Not $status Then __WordErrorNotify("Warning", "_WordDocPrint", _
  920.             "Cannot register internal error handler, cannot trap COM errors", _
  921.             "Use _WordErrorHandlerRegister() to register a user error handler")
  922.     Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
  923.     _WordErrorNotify(False)
  924.     
  925.     If $s_Printer Then
  926.         $s_ActivePrinter = $o_object.Application.ActivePrinter
  927.         $o_object.Application.ActivePrinter = $s_Printer
  928.         If @error = $_WordStatus_ComError And $WordComErrorNumber = -2147352567 And $WordComErrorDescription = "There is a printer error." Then
  929.             $i_ErrorStatusCode = $_WordStatus_InvalidValue
  930.             $s_ErrorMSG = "Invalid printer name specified."
  931.             $i_Extended = 6
  932.         EndIf
  933.     EndIf
  934.     
  935.     $i_From = String($i_From)
  936.     $i_To = String($i_To)
  937.     If Not $i_ErrorStatusCode Then
  938.         $o_object.PrintOut ($f_Background, 0, $i_Range, "", $i_From, $i_To, $i_Item, $i_Copies, $s_Pages, $i_PageType, 0, $f_Collate)
  939.         If @error = $_WordStatus_ComError Then
  940.             $i_ErrorStatusCode = $_WordStatus_ComError
  941.         EndIf
  942.     EndIf
  943.     
  944.     If $i_DocOrientation <> "" And $i_DocOrientation <> $i_Orientation Then
  945.         $o_object.PageSetup.Orientation = $i_DocOrientation
  946.     EndIf
  947.     If $s_ActivePrinter Then
  948.         $o_object.Application.ActivePrinter = $s_ActivePrinter
  949.     EndIf
  950.     
  951.     ; restore error notify and error handler status
  952.     _WordErrorNotify($f_NotifyStatus) ; restore notification status
  953.     __WordInternalErrorHandlerDeRegister()
  954.     
  955.     Switch $i_ErrorStatusCode
  956.         Case $_WordStatus_Success
  957.             SetError($_WordStatus_Success)
  958.             Return 1
  959.         Case $_WordStatus_InvalidValue
  960.             __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", $s_ErrorMSG)
  961.             SetError($_WordStatus_InvalidValue, $i_Extended)
  962.             Return 0
  963.         Case $_WordStatus_ComError
  964.             __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_ComError", "There was an error while executing the 'PrintOut' Method.")
  965.             SetError($_WordStatus_ComError)
  966.             Return 0
  967.         Case Else
  968.             __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_GeneralError", "Invalid Error Status - Notify Word.au3 developer")
  969.             SetError($_WordStatus_GeneralError)
  970.             Return 0
  971.     EndSwitch
  972. EndFunc   ;==>_WordDocPrint
  973.  
  974. ;===============================================================================
  975. ;
  976. ; Function Name:    _WordDocPropertyGet()
  977. ; Description:      Returns a select property of the Word Document.
  978. ; Parameter(s):     $o_object    - Object variable of a Word.Application, document object
  979. ;                    $v_property    - Property selection
  980. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  981. ; Return Value(s):  On Success    - Value of selected Property
  982. ;                   On Failure    - Returns 0 and sets @ERROR
  983. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  984. ;                                - 1 ($_WordStatus_GeneralError) = General Error
  985. ;                                - 2 ($_WordStatus_ComError) = Com Error
  986. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  987. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  988. ;                                - 5 ($_WordStatus_InvalidValue) = Invalid Value
  989. ;                    @Extended    - Contains invalid parameter number
  990. ; Author(s):        Bob Anthony (Code based off IE.au3)
  991. ;
  992. ;===============================================================================
  993. ;
  994. Func _WordDocPropertyGet(ByRef $o_object, $v_property)
  995.     If Not IsObj($o_object) Then
  996.         __WordErrorNotify("Error", "_WordDocPropertyGet", "$_WordStatus_InvalidDataType")
  997.         SetError($_WordStatus_InvalidDataType, 1)
  998.         Return 0
  999.     EndIf
  1000.     ;
  1001.     If Not __WordIsObjType($o_object, "document") Then
  1002.         __WordErrorNotify("Error", "_WordDocPropertyGet", "$_WordStatus_InvalidObjectType")
  1003.         SetError($_WordStatus_InvalidObjectType, 1)
  1004.         Return 0
  1005.     EndIf
  1006.     ;
  1007.     Local $s_Property, $s_ErrorMSG, $i_Extended, $i_ErrorStatusCode = $_WordStatus_Success
  1008.     
  1009.     ; Setup internal error handler to Trap COM errors, turn off error notification
  1010.     Local $status = __WordInternalErrorHandlerRegister()
  1011.     If Not $status Then __WordErrorNotify("Warning", "_WordDocPropertyGet", _
  1012.             "Cannot register internal error handler, cannot trap COM errors", _
  1013.             "Use _WordErrorHandlerRegister() to register a user error handler")
  1014.     Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
  1015.     _WordErrorNotify(False)
  1016.     
  1017.     If IsNumber($v_property) Then
  1018.         Switch $v_property
  1019.             Case 19, 25 To 28
  1020.                 $i_ErrorStatusCode = $_WordStatus_InvalidValue
  1021.                 $s_ErrorMSG = "The specified property is not supported."
  1022.                 $i_Extended = 2
  1023.             Case 1 To 30
  1024.                 $s_Property = $o_object.BuiltInDocumentProperties ($v_property).value
  1025.                 If @error = $_WordStatus_ComError Then
  1026.                     $i_ErrorStatusCode = $_WordStatus_ComError
  1027.                     $s_ErrorMSG = "The specified property has not been defined."
  1028.                     $i_Extended = 2
  1029.                 EndIf
  1030.             Case Else
  1031.                 $i_ErrorStatusCode = $_WordStatus_InvalidValue
  1032.                 $s_ErrorMSG = "The specified property does not exist."
  1033.                 $i_Extended = 2
  1034.         EndSwitch
  1035.     Else
  1036.         Switch $v_property
  1037.             Case "Title", "Subject", "Author", "Keywords", "Comments", "Template", "Last Author", "Revision Number", "Application Name", _
  1038.                     "Last Print Date", "Creation Date", "Last Save Time", "Total Editing Time", "Security", "Category", "Manager", "Company", "Hyperlink base"
  1039.                 $s_Property = $o_object.BuiltInDocumentProperties ($v_property).value
  1040.                 If @error = $_WordStatus_ComError Then
  1041.                     $i_ErrorStatusCode = $_WordStatus_ComError
  1042.                     $s_ErrorMSG = "The specified property has not been defined."
  1043.                     $i_Extended = 2
  1044.                 EndIf
  1045.             Case "Pages", "Words", "Characters", "Characters (with spaces)", "Bytes", "Lines", "Paragraphs"
  1046.                 $s_Property = $o_object.BuiltInDocumentProperties ("Number of " & $v_property).value
  1047.                 If @error = $_WordStatus_ComError Then
  1048.                     $i_ErrorStatusCode = $_WordStatus_ComError
  1049.                     $s_ErrorMSG = "The specified property has not been defined."
  1050.                     $i_Extended = 2
  1051.                 EndIf
  1052.             Case Else
  1053.                 $i_ErrorStatusCode = $_WordStatus_InvalidValue
  1054.                 $s_ErrorMSG = "The specified property does not exist."
  1055.                 $i_Extended = 2
  1056.         EndSwitch
  1057.     EndIf
  1058.     
  1059.     ; restore error notify and error handler status
  1060.     _WordErrorNotify($f_NotifyStatus) ; restore notification status
  1061.     __WordInternalErrorHandlerDeRegister()
  1062.     
  1063.     Switch $i_ErrorStatusCode
  1064.         Case $_WordStatus_Success
  1065.             SetError($_WordStatus_Success)
  1066.             Return $s_Property
  1067.         Case $_WordStatus_InvalidValue
  1068.             __WordErrorNotify("Error", "_WordDocPropertyGet", "$_WordStatus_InvalidValue", $s_ErrorMSG)
  1069.             SetError($_WordStatus_InvalidValue, $i_Extended)
  1070.             Return 0
  1071.         Case $_WordStatus_ComError
  1072.             __WordErrorNotify("Error", "_WordDocPropertyGet", "$_WordStatus_ComError", $s_ErrorMSG)
  1073.             SetError($_WordStatus_ComError, $i_Extended)
  1074.             Return 0
  1075.         Case Else
  1076.             __WordErrorNotify("Error", "_WordDocPropertyGet", "$_WordStatus_GeneralError", "Invalid Error Status - Notify Word.au3 developer")
  1077.             SetError($_WordStatus_GeneralError)
  1078.             Return 0
  1079.     EndSwitch
  1080. EndFunc   ;==>_WordDocPropertyGet
  1081.  
  1082. ;===============================================================================
  1083. ;
  1084. ; Function Name:    _WordDocPropertySet()
  1085. ; Description:      Set a select property of the Word Document.
  1086. ; Parameter(s):     $o_object    - Object variable of a Word.Application, document object
  1087. ;                    $v_property    - Property selection
  1088. ;                    $v_newvalue    - The new value to be set into the Word Document Property
  1089. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  1090. ; Return Value(s):  On Success    - Returns 1
  1091. ;                   On Failure    - Returns 0 and sets @ERROR
  1092. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  1093. ;                                - 1 ($_WordStatus_GeneralError) = General Error
  1094. ;                                - 2 ($_WordStatus_ComError) = Com Error
  1095. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  1096. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  1097. ;                                - 5 ($_WordStatus_InvalidValue) = Invalid Value
  1098. ;                    @Extended    - Contains invalid parameter number
  1099. ; Author(s):        Bob Anthony (Code based off IE.au3)
  1100. ;
  1101. ;===============================================================================
  1102. ;
  1103. Func _WordDocPropertySet(ByRef $o_object, $v_property, $v_newvalue)
  1104.     If Not IsObj($o_object) Then
  1105.         __WordErrorNotify("Error", "_WordDocPropertySet", "$_WordStatus_InvalidDataType")
  1106.         SetError($_WordStatus_InvalidDataType, 1)
  1107.         Return 0
  1108.     EndIf
  1109.     ;
  1110.     If Not __WordIsObjType($o_object, "document") Then
  1111.         __WordErrorNotify("Error", "_WordDocPropertySet", "$_WordStatus_InvalidObjectType")
  1112.         SetError($_WordStatus_InvalidObjectType, 1)
  1113.         Return 0
  1114.     EndIf
  1115.     
  1116.     Local $s_ErrorMSG, $i_Extended, $i_ErrorStatusCode = $_WordStatus_Success
  1117.     
  1118.     ; Setup internal error handler to Trap COM errors, turn off error notification
  1119.     Local $status = __WordInternalErrorHandlerRegister()
  1120.     If Not $status Then __WordErrorNotify("Warning", "_WordDocPropertySet", _
  1121.             "Cannot register internal error handler, cannot trap COM errors", _
  1122.             "Use _WordErrorHandlerRegister() to register a user error handler")
  1123.     Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
  1124.     _WordErrorNotify(False)
  1125.     
  1126.     If IsNumber($v_property) Then
  1127.         Switch $v_property
  1128.             Case 1 To 6, 9, 18, 20, 21, 29
  1129.                 $o_object.BuiltInDocumentProperties ($v_property).value = $v_newvalue
  1130.                 If @error = $_WordStatus_ComError Then
  1131.                     $i_ErrorStatusCode = $_WordStatus_ComError
  1132.                     $s_ErrorMSG = "There was an error while setting the selected property."
  1133.                     $i_Extended = 3
  1134.                 EndIf
  1135.             Case 1 To 30
  1136.                 $i_ErrorStatusCode = $_WordStatus_InvalidValue
  1137.                 $s_ErrorMSG = "The specified property is not supported."
  1138.                 $i_Extended = 2
  1139.             Case Else
  1140.                 $i_ErrorStatusCode = $_WordStatus_InvalidValue
  1141.                 $s_ErrorMSG = "The specified property does not exist."
  1142.                 $i_Extended = 2
  1143.         EndSwitch
  1144.     Else
  1145.         Switch $v_property
  1146.             Case "Title", "Subject", "Author", "Keywords", "Comments", "Template", _
  1147.                     "Application Name", "Category", "Manager", "Company", "Hyperlink base"
  1148.                 $o_object.BuiltInDocumentProperties ($v_property).value = $v_newvalue
  1149.                 If @error = $_WordStatus_ComError Then
  1150.                     $i_ErrorStatusCode = $_WordStatus_ComError
  1151.                     $s_ErrorMSG = "There was an error while setting the selected property."
  1152.                     $i_Extended = 3
  1153.                 EndIf
  1154.             Case Else
  1155.                 $i_ErrorStatusCode = $_WordStatus_InvalidValue
  1156.                 $s_ErrorMSG = "The specified property does not exist."
  1157.                 $i_Extended = 2
  1158.         EndSwitch
  1159.     EndIf
  1160.     
  1161.     ; restore error notify and error handler status
  1162.     _WordErrorNotify($f_NotifyStatus) ; restore notification status
  1163.     __WordInternalErrorHandlerDeRegister()
  1164.     
  1165.     Switch $i_ErrorStatusCode
  1166.         Case $_WordStatus_Success
  1167.             SetError($_WordStatus_Success)
  1168.             Return 1
  1169.         Case $_WordStatus_InvalidValue
  1170.             __WordErrorNotify("Error", "_WordDocPropertySet", "$_WordStatus_InvalidValue", $s_ErrorMSG)
  1171.             SetError($_WordStatus_InvalidValue, $i_Extended)
  1172.             Return 0
  1173.         Case $_WordStatus_ComError
  1174.             __WordErrorNotify("Error", "_WordDocPropertySet", "$_WordStatus_ComError", $s_ErrorMSG)
  1175.             SetError($_WordStatus_ComError, $i_Extended)
  1176.             Return 0
  1177.         Case Else
  1178.             __WordErrorNotify("Error", "_WordDocPropertySet", "$_WordStatus_GeneralError", "Invalid Error Status - Notify Word.au3 developer")
  1179.             SetError($_WordStatus_GeneralError)
  1180.             Return 0
  1181.     EndSwitch
  1182. EndFunc   ;==>_WordDocPropertySet
  1183.  
  1184. ;===============================================================================
  1185. ;
  1186. ; Function Name:    _WordDocLinkGetCollection()
  1187. ; Description:        Returns a collection object containing all links in the document
  1188. ; Parameter(s):        $o_object    - Object variable of an Word.Application, document object
  1189. ;                    $i_index    - Optional: specifies whether to return a collection or indexed instance
  1190. ;                                - Positive integer returns an indexed instance (1 based)
  1191. ;                                - -1 = (Default) returns a collection
  1192. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  1193. ; Return Value(s):  On Success     - Returns an object collection of all links in the document, @EXTENDED = link count
  1194. ;                   On Failure    - Returns 0 and sets @ERROR
  1195. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  1196. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  1197. ;                                - 5 ($_WordStatus_InvalidValue) = Invalid Value
  1198. ;                                - 7 ($_WordStatus_NoMatch) = No Match
  1199. ;                    @Extended    - Contains invalid parameter number
  1200. ; Author(s):        Bob Anthony (Code based off IE.au3)
  1201. ;
  1202. ;===============================================================================
  1203. ;
  1204. Func _WordDocLinkGetCollection(ByRef $o_object, $i_index = -1)
  1205.     If Not IsObj($o_object) Then
  1206.         __WordErrorNotify("Error", "_WordDocLinkGetCollection", "$_WordStatus_InvalidDataType")
  1207.         SetError($_WordStatus_InvalidDataType, 1)
  1208.         Return 0
  1209.     EndIf
  1210.     ;
  1211.     If Not __WordIsObjType($o_object, "document") Then
  1212.         __WordErrorNotify("Error", "_WordDocLinkGetCollection", "$_WordStatus_InvalidObjectType")
  1213.         SetError($_WordStatus_InvalidObjectType, 1)
  1214.         Return 0
  1215.     EndIf
  1216.     ;
  1217.     $i_index = Number($i_index)
  1218.     Select
  1219.         Case $i_index = -1
  1220.             SetError($_WordStatus_Success)
  1221.             SetExtended($o_object.Hyperlinks.Count)
  1222.             Return $o_object.Hyperlinks
  1223.         Case $i_index > 0 And $i_index <= $o_object.Hyperlinks.Count
  1224.             SetError($_WordStatus_Success)
  1225.             SetExtended($o_object.Hyperlinks.Count)
  1226.             Return $o_object.Hyperlinks.Item ($i_index)
  1227.         Case $i_index < -1 Or $i_index = 0
  1228.             __WordErrorNotify("Error", "_WordDocLinkGetCollection", "$_WordStatus_InvalidValue")
  1229.             SetError($_WordStatus_InvalidValue, 2)
  1230.             Return 0
  1231.         Case Else
  1232.             __WordErrorNotify("Warning", "_WordDocLinkGetCollection", "$_WordStatus_NoMatch")
  1233.             SetError($_WordStatus_NoMatch, 2)
  1234.             Return 0
  1235.     EndSelect
  1236. EndFunc   ;==>_WordDocLinkGetCollection
  1237.  
  1238. ;===============================================================================
  1239. ;
  1240. ; Function Name:    _WordDocAddLink()
  1241. ; Description:      Add a hyperlink to the document
  1242. ; Parameter(s):     $o_object            - Object variable of a Word.Application, document object
  1243. ;                    $o_Anchor            - Optional: The text or graphic that you want turned into a hyperlink.
  1244. ;                                            "" = (Default) Uses entire document as range
  1245. ;                    $s_Address            - Optional: The address for the specified link. The address can be an
  1246. ;                                            e-mail address, an Internet address, or a file name.
  1247. ;                                            "" = (Default) Link to the specified document is used
  1248. ;                    $s_SubAddress        - Optional: The name of a location within the destination file, such as
  1249. ;                                            a bookmark, named range, or slide number.
  1250. ;                    $s_ScreenTip        - Optional: The text that appears as a ScreenTip when the mouse pointer is
  1251. ;                                            positioned over the specified hyperlink.
  1252. ;                                            "" = (Default) Uses value of $s_Address
  1253. ;                    $s_TextToDisplay    - Optional: The display text of the specified hyperlink. The value of this
  1254. ;                                            argument replaces the text or graphic specified by Anchor.
  1255. ;                                            "" = (Default) Uses value of $s_Address
  1256. ;                    $s_Target            - Optional: The name of the frame or window in which you want to load the specified hyperlink.
  1257. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  1258. ; Return Value(s):  On Success    - Returns 1
  1259. ;                   On Failure    - Returns 0 and sets @ERROR
  1260. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  1261. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  1262. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  1263. ;                    @Extended    - Contains invalid parameter number
  1264. ; Author(s):        Bob Anthony (Code based off IE.au3)
  1265. ;
  1266. ;===============================================================================
  1267. ;
  1268. Func _WordDocAddLink(ByRef $o_object, $o_Anchor = "", $s_Address = "", $s_SubAddress = "", $s_ScreenTip = "", $s_TextToDisplay = "", $s_Target = "")
  1269.     If Not IsObj($o_object) Then
  1270.         __WordErrorNotify("Error", "_WordDocAddLink", "$_WordStatus_InvalidDataType")
  1271.         SetError($_WordStatus_InvalidDataType, 1)
  1272.         Return 0
  1273.     EndIf
  1274.     ;
  1275.     If Not __WordIsObjType($o_object, "document") Then
  1276.         __WordErrorNotify("Error", "_WordDocAddLink", "$_WordStatus_InvalidObjectType")
  1277.         SetError($_WordStatus_InvalidObjectType, 1)
  1278.         Return 0
  1279.     EndIf
  1280.     
  1281.     If $o_Anchor = "" Then
  1282.         $o_Anchor = $o_object.Range
  1283.     EndIf
  1284.     
  1285.     If $s_Address = "" Then
  1286.         $s_Address = $o_object.FullName
  1287.     EndIf
  1288.     
  1289.     $o_object.Hyperlinks.Add ($o_Anchor, $s_Address, $s_SubAddress, $s_ScreenTip, $s_TextToDisplay, $s_Target)
  1290.     SetError($_WordStatus_Success)
  1291.     Return 1
  1292. EndFunc   ;==>_WordDocAddLink
  1293.  
  1294. ;===============================================================================
  1295. ;
  1296. ; Function Name:    _WordDocAddPicture()
  1297. ; Description:      Add a picture to the document
  1298. ; Parameter(s):     $o_object            - Object variable of a Word.Application, document object.
  1299. ;                    $s_FilePath            - The path and file name of the picture.
  1300. ;                    $f_LinkToFile        - Optional: Specifies whether to link the picture to the file from which it was created.
  1301. ;                                            0 = (Default) Make the picture an independent copy of the file
  1302. ;                                            1 = Link the picture to the file from which it was created
  1303. ;                    $f_SaveWithDocument    - Optional: Specifies whether to save the linked picture with the document.
  1304. ;                                            0 = (Default) Do not save the linked picture with the document
  1305. ;                                            1 = Save the linked picture with the document
  1306. ;                    $o_Range            - Optional: The location where the picture will be placed in the text.
  1307. ;                                            "" = (Default) The picture is placed automatically
  1308. ;                                            Any range object
  1309. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  1310. ; Return Value(s):  On Success    - Returns an object variable pointing to a Word.Application, shape object
  1311. ;                   On Failure    - Returns 0 and sets @ERROR
  1312. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  1313. ;                                - 1 ($_WordStatus_GeneralError) = General Error
  1314. ;                                - 2 ($_WordStatus_ComError) = Com Error
  1315. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  1316. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  1317. ;                                - 5 ($_WordStatus_InvalidValue) = Invalid Value
  1318. ;                    @Extended    - Contains invalid parameter number
  1319. ; Author(s):        Bob Anthony (Code based off IE.au3)
  1320. ;
  1321. ;===============================================================================
  1322. ;
  1323. Func _WordDocAddPicture(ByRef $o_object, $s_FilePath, $f_LinkToFile = 0, $f_SaveWithDocument = 0, $o_Range = "")
  1324.     If Not IsObj($o_object) Then
  1325.         __WordErrorNotify("Error", "_WordDocAddPicture", "$_WordStatus_InvalidDataType")
  1326.         SetError($_WordStatus_InvalidDataType, 1)
  1327.         Return 0
  1328.     EndIf
  1329.     ;
  1330.     If Not __WordIsObjType($o_object, "document") Then
  1331.         __WordErrorNotify("Error", "_WordDocAddPicture", "$_WordStatus_InvalidObjectType")
  1332.         SetError($_WordStatus_InvalidObjectType, 1)
  1333.         Return 0
  1334.     EndIf
  1335.     ;
  1336.     If Not FileExists($s_FilePath) Then
  1337.         __WordErrorNotify("Error", "_WordDocAddPicture", "$_WordStatus_InvalidValue", "The specified file does not exist.")
  1338.         SetError($_WordStatus_InvalidValue, 2)
  1339.         Return 0
  1340.     EndIf
  1341.     ;
  1342.     Local $o_Shape, $f_Range, $i_ErrorStatusCode = $_WordStatus_Success
  1343.     
  1344.     If $o_Range = "" Then
  1345.         $f_Range = False
  1346.     Else
  1347.         If Not __WordIsObjType($o_Range, "range") Then
  1348.             __WordErrorNotify("Error", "_WordDocAddPicture", "$_WordStatus_InvalidObjectType")
  1349.             SetError($_WordStatus_InvalidObjectType, 5)
  1350.             Return 0
  1351.         EndIf
  1352.         $f_Range = True
  1353.     EndIf
  1354.  
  1355.     ; Setup internal error handler to Trap COM errors, turn off error notification
  1356.     Local $status = __WordInternalErrorHandlerRegister()
  1357.     If Not $status Then __WordErrorNotify("Warning", "_WordDocAddPicture", _
  1358.             "Cannot register internal error handler, cannot trap COM errors", _
  1359.             "Use _WordErrorHandlerRegister() to register a user error handler")
  1360.     Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
  1361.     _WordErrorNotify(False)
  1362.     
  1363.     If $f_Range Then
  1364.         $o_Shape = $o_object.InlineShapes.AddPicture ($s_FilePath, $f_LinkToFile, $f_SaveWithDocument, $o_Range)
  1365.     Else
  1366.         $o_Shape = $o_object.InlineShapes.AddPicture ($s_FilePath, $f_LinkToFile, $f_SaveWithDocument)
  1367.     EndIf
  1368.     If @error = $_WordStatus_ComError Then $i_ErrorStatusCode = $_WordStatus_ComError
  1369.     
  1370.     ; restore error notify and error handler status
  1371.     _WordErrorNotify($f_NotifyStatus) ; restore notification status
  1372.     __WordInternalErrorHandlerDeRegister()
  1373.     
  1374.     Switch $i_ErrorStatusCode
  1375.         Case $_WordStatus_Success
  1376.             SetError($_WordStatus_Success)
  1377.             Return $o_Shape
  1378.         Case $_WordStatus_ComError
  1379.             __WordErrorNotify("Error", "_WordDocAddPicture", "$_WordStatus_ComError", "There was an error while executing the 'AddPicture' Method.")
  1380.             SetError($_WordStatus_ComError)
  1381.             Return 0
  1382.         Case Else
  1383.             __WordErrorNotify("Error", "_WordDocAddPicture", "$_WordStatus_GeneralError", "Invalid Error Status - Notify Word.au3 developer")
  1384.             SetError($_WordStatus_GeneralError)
  1385.             Return 0
  1386.     EndSwitch
  1387. EndFunc   ;==>_WordDocAddPicture
  1388. #endregion
  1389. #region Error Handling
  1390. ;===============================================================================
  1391. ;
  1392. ; Function Name:   _WordErrorHandlerRegister()
  1393. ; Description:        Register and enable a user COM error handler
  1394. ; Parameter(s):        $s_functionName - String variable with the name of a user-defined COM error handler
  1395. ;                                      defaults to the internal COM error handler in this UDF
  1396. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  1397. ; Return Value(s):  On Success     - Returns 1
  1398. ;                   On Failure    - Returns 0 and sets @ERROR
  1399. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  1400. ;                                - 1 ($_WordStatus_GeneralError) = General Error
  1401. ;                    @Extended    - Contains invalid parameter number
  1402. ; Author(s):        Bob Anthony (Code based off IE.au3)
  1403. ;
  1404. ;===============================================================================
  1405. ;
  1406. Func _WordErrorHandlerRegister($s_functionName = "__WordInternalErrorHandler")
  1407.     $sWordUserErrorHandler = $s_functionName
  1408.     $oWordErrorHandler = ""
  1409.     $oWordErrorHandler = ObjEvent("AutoIt.Error", $s_functionName)
  1410.     If IsObj($oWordErrorHandler) Then
  1411.         SetError($_WordStatus_Success)
  1412.         Return 1
  1413.     Else
  1414.         __WordErrorNotify("Error", "_WordErrorHandlerRegister", "$_WordStatus_GeneralError", _
  1415.                 "Error Handler Not Registered - Check existance of error function")
  1416.         SetError($_WordStatus_GeneralError, 1)
  1417.         Return 0
  1418.     EndIf
  1419. EndFunc   ;==>_WordErrorHandlerRegister
  1420.  
  1421. ;===============================================================================
  1422. ;
  1423. ; Function Name:   _WordErrorHandlerDeRegister()
  1424. ; Description:        Disable a registered user COM error handler
  1425. ; Parameter(s):        None
  1426. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  1427. ; Return Value(s):  On Success     - Returns 1
  1428. ;                   On Failure    - None
  1429. ; Author(s):        Bob Anthony (Code based off IE.au3)
  1430. ;
  1431. ;===============================================================================
  1432. ;
  1433. Func _WordErrorHandlerDeRegister()
  1434.     $sWordUserErrorHandler = ""
  1435.     $oWordErrorHandler = ""
  1436.     SetError($_WordStatus_Success)
  1437.     Return 1
  1438. EndFunc   ;==>_WordErrorHandlerDeRegister
  1439. #endregion
  1440. #region Utility Functions
  1441. ;===============================================================================
  1442. ;
  1443. ; Function Name:   _WordErrorNotify()
  1444. ; Description:        Specifies whether Word.au3 automatically notifies of Warnings and Errors (to the console)
  1445. ; Parameter(s):        $f_notify    - Optional: specifies whether notification should be on or off
  1446. ;                                - -1 = (Default) return current setting
  1447. ;                                - True = Turn On
  1448. ;                                - False = Turn Off
  1449. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  1450. ; Return Value(s):  On Success     - If $f_notify = -1, returns the current notification setting, else returns 1
  1451. ;                   On Failure    - Returns 0
  1452. ; Author(s):        Bob Anthony (Code based off IE.au3)
  1453. ;
  1454. ;===============================================================================
  1455. ;
  1456. Func _WordErrorNotify($f_notify = -1)
  1457.     Switch Number($f_notify)
  1458.         Case (-1)
  1459.             Return $_WordErrorNotify
  1460.         Case 0
  1461.             $_WordErrorNotify = False
  1462.             Return 1
  1463.         Case 1
  1464.             $_WordErrorNotify = True
  1465.             Return 1
  1466.         Case Else
  1467.             __WordErrorNotify("Error", "_WordErrorNotify", "$_WordStatus_InvalidValue")
  1468.             Return 0
  1469.     EndSwitch
  1470. EndFunc   ;==>_WordErrorNotify
  1471.  
  1472. ;===============================================================================
  1473. ;
  1474. ; Function Name:    _WordMacroRun()
  1475. ; Description:      Runs a Visual Basic macro
  1476. ; Parameter(s):     $o_object            - Object variable of a Word.Application object
  1477. ;                    $s_MacroName        - The name of the macro. Can be any combination of template,
  1478. ;                                            module, and macro name. (See Remarks)
  1479. ;                    $v_Arg1                - Optional: The first parameter to pass to the macro
  1480. ;                    ...                    ...
  1481. ;                    $v_Arg30            - Optional: The thirtieth parameter to pass to the macro
  1482. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  1483. ; Return Value(s):  On Success    - Returns 1
  1484. ;                   On Failure    - Returns 0 and sets @ERROR
  1485. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  1486. ;                                - 1 ($_WordStatus_GeneralError) = General Error
  1487. ;                                - 2 ($_WordStatus_ComError) = Com Error
  1488. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  1489. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  1490. ;                    @Extended    - Contains invalid parameter number
  1491. ; Remark(s):        If you specify the document name, your code can only run macros in documents
  1492. ;                    related to the current context รน not just any macro in any document.
  1493. ; Author(s):        Bob Anthony (Code based off IE.au3)
  1494. ;
  1495. ;===============================================================================
  1496. ;
  1497. Func _WordMacroRun(ByRef $o_object, $s_MacroName, $v_Arg1 = Default, $v_Arg2 = Default, $v_Arg3 = Default, $v_Arg4 = Default, $v_Arg5 = Default, $v_Arg6 = Default, $v_Arg7 = Default, $v_Arg8 = Default, $v_Arg9 = Default, $v_Arg10 = Default, $v_Arg11 = Default, $v_Arg12 = Default, $v_Arg13 = Default, $v_Arg14 = Default, $v_Arg15 = Default, $v_Arg16 = Default, $v_Arg17 = Default, $v_Arg18 = Default, $v_Arg19 = Default, $v_Arg20 = Default, $v_Arg21 = Default, $v_Arg22 = Default, $v_Arg23 = Default, $v_Arg24 = Default, $v_Arg25 = Default, $v_Arg26 = Default, $v_Arg27 = Default, $v_Arg28 = Default, $v_Arg29 = Default, $v_Arg30 = Default)
  1498.  
  1499.     If Not IsObj($o_object) Then
  1500.         __WordErrorNotify("Error", "_WordMacroRun", "$_WordStatus_InvalidDataType")
  1501.         SetError($_WordStatus_InvalidDataType, 1)
  1502.         Return 0
  1503.     EndIf
  1504.     ;
  1505.     If Not __WordIsObjType($o_object, "application") Then
  1506.         __WordErrorNotify("Error", "_WordMacroRun", "$_WordStatus_InvalidObjectType")
  1507.         SetError($_WordStatus_InvalidObjectType, 1)
  1508.         Return 0
  1509.     EndIf
  1510.     ;
  1511.     Local $s_ErrorMSG, $i_Extended, $i_ErrorStatusCode = $_WordStatus_Success
  1512.     
  1513.     ; Setup internal error handler to Trap COM errors, turn off error notification
  1514.     Local $status = __WordInternalErrorHandlerRegister()
  1515.     If Not $status Then __WordErrorNotify("Warning", "_WordMacroRun", _
  1516.             "Cannot register internal error handler, cannot trap COM errors", _
  1517.             "Use _WordErrorHandlerRegister() to register a user error handler")
  1518.     Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
  1519.     _WordErrorNotify(False)
  1520.     
  1521.     $o_object.Run($s_MacroName, $v_Arg1, $v_Arg2, $v_Arg3, $v_Arg4, $v_Arg5, _
  1522.             $v_Arg6, $v_Arg7, $v_Arg8, $v_Arg9, $v_Arg10, _
  1523.             $v_Arg11, $v_Arg12, $v_Arg13, $v_Arg14, $v_Arg15, _
  1524.             $v_Arg16, $v_Arg17, $v_Arg18, $v_Arg19, $v_Arg20, _
  1525.             $v_Arg21, $v_Arg22, $v_Arg23, $v_Arg24, $v_Arg25, _
  1526.             $v_Arg26, $v_Arg27, $v_Arg28, $v_Arg29, $v_Arg30)
  1527.     
  1528.     If @error = $_WordStatus_ComError Then
  1529.         If $WordComErrorNumber = -2147352567 Then
  1530.             $i_ErrorStatusCode = $_WordStatus_InvalidValue
  1531.             Switch $WordComErrorWinDescription
  1532.                 Case "Member not found."
  1533.                     $s_ErrorMSG = "The specified macro does not exist."
  1534.                     $i_Extended = 2
  1535.                 Case "Invalid number of parameters."
  1536.                     $s_ErrorMSG = "Invalid number of parameters."
  1537.                     $i_Extended = -1
  1538.             EndSwitch
  1539.         Else
  1540.             $i_ErrorStatusCode = $_WordStatus_ComError
  1541.         EndIf
  1542.     EndIf
  1543. ;~     ConsoleWrite("Error: " & $WordComErrorNumber & @CR)
  1544. ;~     ConsoleWrite("Desc: " & $WordComErrorWinDescription & @CR)
  1545.     
  1546.     ; restore error notify and error handler status
  1547.     _WordErrorNotify($f_NotifyStatus) ; restore notification status
  1548.     __WordInternalErrorHandlerDeRegister()
  1549.     
  1550.     Switch $i_ErrorStatusCode
  1551.         Case $_WordStatus_Success
  1552.             SetError($_WordStatus_Success)
  1553.             Return 1
  1554.         Case $_WordStatus_InvalidValue
  1555.             __WordErrorNotify("Error", "_WordMacroRun", "$_WordStatus_InvalidValue", $s_ErrorMSG)
  1556.             SetError($_WordStatus_InvalidValue, $i_Extended)
  1557.             Return 0
  1558.         Case $_WordStatus_ComError
  1559.             __WordErrorNotify("Error", "_WordMacroRun", "$_WordStatus_ComError", "There was an error while executing the 'Run' Method.")
  1560.             SetError($_WordStatus_ComError)
  1561.             Return 0
  1562.         Case Else
  1563.             __WordErrorNotify("Error", "_WordMacroRun", "$_WordStatus_GeneralError", "Invalid Error Status - Notify Word.au3 developer.")
  1564.             SetError($_WordStatus_GeneralError)
  1565.             Return 0
  1566.     EndSwitch
  1567. EndFunc   ;==>_WordMacroRun
  1568.  
  1569. ;===============================================================================
  1570. ;
  1571. ; Function Name:    _WordPropertyGet()
  1572. ; Description:      Returns a select property of the Word Application
  1573. ; Parameter(s):     $o_object    - Object variable of a Word.Application
  1574. ;                    $s_property    - Property selection
  1575. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  1576. ; Return Value(s):  On Success    - Value of selected Property
  1577. ;                   On Failure    - Returns 0 and sets @ERROR
  1578. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  1579. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  1580. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  1581. ;                                - 5 ($_WordStatus_InvalidValue) = Invalid Value
  1582. ;                    @Extended    - Contains invalid parameter number
  1583. ; Author(s):        Bob Anthony (Code based off IE.au3)
  1584. ;
  1585. ;===============================================================================
  1586. ;
  1587. Func _WordPropertyGet(ByRef $o_object, $s_Property)
  1588.     If Not IsObj($o_object) Then
  1589.         __WordErrorNotify("Error", "_WordPropertyGet", "$_WordStatus_InvalidDataType")
  1590.         SetError($_WordStatus_InvalidDataType, 1)
  1591.         Return 0
  1592.     EndIf
  1593.     ;
  1594.     If Not __WordIsObjType($o_object, "wordobj") Then
  1595.         __WordErrorNotify("Error", "_WordPropertyGet", "$_WordStatus_InvalidObjectType")
  1596.         SetError($_WordStatus_InvalidObjectType, 1)
  1597.         Return 0
  1598.     EndIf
  1599.     
  1600.     $s_Property = StringLower($s_Property)
  1601.     Switch $s_Property
  1602.         Case "activeprinter"
  1603.             SetError($_WordStatus_Success)
  1604.             Return $o_object.Application.ActivePrinter ()
  1605.         Case "capslock"
  1606.             SetError($_WordStatus_Success)
  1607.             Return $o_object.Application.CapsLock ()
  1608.         Case "screentips"
  1609.             If __WordIsObjType($o_object, "window") Then
  1610.                 SetError($_WordStatus_Success)
  1611.                 Return $o_object.DisplayScreenTips ()
  1612.             Else
  1613.                 SetError($_WordStatus_Success)
  1614.                 Return $o_object.Application.DisplayScreenTips ()
  1615.             EndIf
  1616.         Case "scrollbars"
  1617.             SetError($_WordStatus_Success)
  1618.             Return $o_object.Application.DisplayScrollBars ()
  1619.         Case "statusbar"
  1620.             SetError($_WordStatus_Success)
  1621.             Return $o_object.Application.DisplayStatusBar ()
  1622.         Case "height"
  1623.             If __WordIsObjType($o_object, "window") Then
  1624.                 SetError($_WordStatus_Success)
  1625.                 Return $o_object.Height ()
  1626.             Else
  1627.                 SetError($_WordStatus_Success)
  1628.                 Return $o_object.Application.Height ()
  1629.             EndIf
  1630.         Case "language"
  1631.             SetError($_WordStatus_Success)
  1632.             Return $o_object.Application.Language ()
  1633.         Case "left"
  1634.             If __WordIsObjType($o_object, "window") Then
  1635.                 SetError($_WordStatus_Success)
  1636.                 Return $o_object.Left ()
  1637.             Else
  1638.                 SetError($_WordStatus_Success)
  1639.                 Return $o_object.Application.Left ()
  1640.             EndIf
  1641.         Case "numlock"
  1642.             SetError($_WordStatus_Success)
  1643.             Return $o_object.Application.Numlock ()
  1644.         Case "path"
  1645.             If __WordIsObjType($o_object, "document") Then
  1646.                 SetError($_WordStatus_Success)
  1647.                 Return $o_object.Path ()
  1648.             Else
  1649.                 SetError($_WordStatus_Success)
  1650.                 Return $o_object.Application.Path ()
  1651.             EndIf
  1652.         Case "screenupdating"
  1653.             SetError($_WordStatus_Success)
  1654.             Return $o_object.Application.ScreenUpdating ()
  1655.         Case "startuppath"
  1656.             SetError($_WordStatus_Success)
  1657.             Return $o_object.Application.StartupPath ()
  1658.         Case "top"
  1659.             If __WordIsObjType($o_object, "window") Then
  1660.                 SetError($_WordStatus_Success)
  1661.                 Return $o_object.Top ()
  1662.             Else
  1663.                 SetError($_WordStatus_Success)
  1664.                 Return $o_object.Application.Top ()
  1665.             EndIf
  1666.         Case "version"
  1667.             SetError($_WordStatus_Success)
  1668.             Return $o_object.Application.Version ()
  1669.         Case "visible"
  1670.             If __WordIsObjType($o_object, "window") Then
  1671.                 SetError($_WordStatus_Success)
  1672.                 Return $o_object.Visible ()
  1673.             Else
  1674.                 SetError($_WordStatus_Success)
  1675.                 Return $o_object.Application.Visible ()
  1676.             EndIf
  1677.         Case "width"
  1678.             If __WordIsObjType($o_object, "window") Then
  1679.                 SetError($_WordStatus_Success)
  1680.                 Return $o_object.Width ()
  1681.             Else
  1682.                 SetError($_WordStatus_Success)
  1683.                 Return $o_object.Application.Width ()
  1684.             EndIf
  1685.         Case "windowstate"
  1686.             If __WordIsObjType($o_object, "window") Then
  1687.                 SetError($_WordStatus_Success)
  1688.                 Return $o_object.WindowState ()
  1689.             Else
  1690.                 SetError($_WordStatus_Success)
  1691.                 Return $o_object.Application.WindowState ()
  1692.             EndIf
  1693.         Case Else
  1694.             ; Unsupported Property
  1695.             __WordErrorNotify("Error", "_WordPropertyGet", "$_WordStatus_InvalidValue", "Invalid Property")
  1696.             SetError($_WordStatus_InvalidValue, 2)
  1697.             Return 0
  1698.     EndSwitch
  1699. EndFunc   ;==>_WordPropertyGet
  1700.  
  1701. ;===============================================================================
  1702. ;
  1703. ; Function Name:    _WordPropertySet()
  1704. ; Description:      Set a select property of the Word Application
  1705. ; Parameter(s):     $o_object    - Object variable of a Word.Application Object
  1706. ;                    $s_property    - Property selection
  1707. ;                    $v_newvalue    - The new value to be set into the Word Application Property
  1708. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  1709. ; Return Value(s):  On Success    - Returns 1
  1710. ;                   On Failure    - Returns 0 and sets @ERROR
  1711. ;                    @ERROR        - 0 ($_WordStatus_Success) = No Error
  1712. ;                                - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
  1713. ;                                - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
  1714. ;                                - 5 ($_WordStatus_InvalidValue) = Invalid Value
  1715. ;                    @Extended    - Contains invalid parameter number
  1716. ; Author(s):        Bob Anthony (Code based off IE.au3)
  1717. ;
  1718. ;===============================================================================
  1719. ;
  1720. Func _WordPropertySet(ByRef $o_object, $s_Property, $v_newvalue)
  1721.     If Not IsObj($o_object) Then
  1722.         __WordErrorNotify("Error", "_WordPropertySet", "$_WordStatus_InvalidDataType")
  1723.         SetError($_WordStatus_InvalidDataType, 1)
  1724.         Return 0
  1725.     EndIf
  1726.     ;
  1727.     If Not __WordIsObjType($o_object, "wordobj") Then
  1728.         __WordErrorNotify("Error", "_WordPropertySet", "$_WordStatus_InvalidObjectType")
  1729.         SetError($_WordStatus_InvalidObjectType, 1)
  1730.         Return 0
  1731.     EndIf
  1732.     
  1733.     $s_Property = StringLower($s_Property)
  1734.     Switch $s_Property
  1735.         Case "activeprinter"
  1736.             SetError($_WordStatus_Success)
  1737.             $o_object.Application.ActivePrinter = $v_newvalue
  1738.             Return 1
  1739.         Case "screentips"
  1740.             If __WordIsObjType($o_object, "window") Then
  1741.                 SetError($_WordStatus_Success)
  1742.                 $o_object.DisplayScreenTips = $v_newvalue
  1743.                 Return 1
  1744.             Else
  1745.                 SetError($_WordStatus_Success)
  1746.                 $o_object.Application.DisplayScreenTips = $v_newvalue
  1747.                 Return 1
  1748.             EndIf
  1749.         Case "scrollbars"
  1750.             SetError($_WordStatus_Success)
  1751.             $o_object.Application.DisplayScrollBars = $v_newvalue
  1752.             Return 1
  1753.         Case "statusbar"
  1754.             SetError($_WordStatus_Success)
  1755.             $o_object.Application.DisplayStatusBar = $v_newvalue
  1756.             Return 1
  1757.         Case "height"
  1758.             If __WordIsObjType($o_object, "window") Then
  1759.                 SetError($_WordStatus_Success)
  1760.                 $o_object.Height = $v_newvalue
  1761.                 Return 1
  1762.             Else
  1763.                 SetError($_WordStatus_Success)
  1764.                 $o_object.Application.Height = $v_newvalue
  1765.                 Return 1
  1766.             EndIf
  1767.         Case "left"
  1768.             If __WordIsObjType($o_object, "window") Then
  1769.                 SetError($_WordStatus_Success)
  1770.                 $o_object.Left = $v_newvalue
  1771.                 Return 1
  1772.             Else
  1773.                 SetError($_WordStatus_Success)
  1774.                 $o_object.Application.Left = $v_newvalue
  1775.                 Return 1
  1776.             EndIf
  1777.         Case "screenupdating"
  1778.             SetError($_WordStatus_Success)
  1779.             $o_object.Application.ScreenUpdating = $v_newvalue
  1780.             Return 1
  1781.         Case "startuppath"
  1782.             SetError($_WordStatus_Success)
  1783.             $o_object.AApplication.StartupPath = $v_newvalue
  1784.             Return 1
  1785.         Case "top"
  1786.             If __WordIsObjType($o_object, "window") Then
  1787.                 SetError($_WordStatus_Success)
  1788.                 $o_object.Top = $v_newvalue
  1789.                 Return 1
  1790.             Else
  1791.                 SetError($_WordStatus_Success)
  1792.                 $o_object.Application.Top = $v_newvalue
  1793.                 Return 1
  1794.             EndIf
  1795.         Case "visible"
  1796.             If __WordIsObjType($o_object, "window") Then
  1797.                 SetError($_WordStatus_Success)
  1798.                 $o_object.visible = $v_newvalue
  1799.                 Return 1
  1800.             Else
  1801.                 SetError($_WordStatus_Success)
  1802.                 $o_object.Application.Visible = $v_newvalue
  1803.                 Return 1
  1804.             EndIf
  1805.         Case "width"
  1806.             If __WordIsObjType($o_object, "window") Then
  1807.                 SetError($_WordStatus_Success)
  1808.                 $o_object.Width = $v_newvalue
  1809.                 Return 1
  1810.             Else
  1811.                 SetError($_WordStatus_Success)
  1812.                 $o_object.Application.Width = $v_newvalue
  1813.                 Return 1
  1814.             EndIf
  1815.         Case "windowstate"
  1816.             If __WordIsObjType($o_object, "window") Then
  1817.                 SetError($_WordStatus_Success)
  1818.                 $o_object.WindowState = $v_newvalue
  1819.                 Return 1
  1820.             Else
  1821.                 SetError($_WordStatus_Success)
  1822.                 $o_object.Application.WindowState = $v_newvalue
  1823.                 Return 1
  1824.             EndIf
  1825.         Case Else
  1826.             ; Unsupported Property
  1827.             __WordErrorNotify("Error", "_WordPropertySet", "$_WordStatus_InvalidValue", "Invalid Property")
  1828.             SetError($_WordStatus_InvalidValue, 2)
  1829.             Return 0
  1830.     EndSwitch
  1831. EndFunc   ;==>_WordPropertySet
  1832. #endregion
  1833. #region General Functions
  1834. ;===============================================================================
  1835. ;
  1836. ; Function Name:    _Word_VersionInfo()
  1837. ; Description:        Returns an array of information about the Word.au3 version
  1838. ; Parameter(s):     None
  1839. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  1840. ; Return Value(s):  On Success     - Returns an array ($WordAU3VersionInfo)
  1841. ;                                - $WordAU3VersionInfo[0] = Release Type (T=Test or V=Production)
  1842. ;                                - $WordAU3VersionInfo[1] = Major Version
  1843. ;                                - $WordAU3VersionInfo[2] = Minor Version
  1844. ;                                - $WordAU3VersionInfo[3] = Sub Version
  1845. ;                                - $WordAU3VersionInfo[4] = Release Date (YYYYMMDD)
  1846. ;                                - $WordAU3VersionInfo[5] = Display Version (e.g. T0.1-0)
  1847. ;                   On Failure    - None
  1848. ; Author(s):        Bob Anthony (Code based off IE.au3)
  1849. ;
  1850. ;===============================================================================
  1851. ;
  1852. Func _Word_VersionInfo()
  1853.     __WordErrorNotify("Information", "_Word_VersionInfo", "version " & _
  1854.             $WordAU3VersionInfo[0] & _
  1855.             $WordAU3VersionInfo[1] & "." & _
  1856.             $WordAU3VersionInfo[2] & "-" & _
  1857.             $WordAU3VersionInfo[3], "Release date: " & $WordAU3VersionInfo[4])
  1858.     SetError($_WordStatus_Success)
  1859.     Return $WordAU3VersionInfo
  1860. EndFunc   ;==>_Word_VersionInfo
  1861. #endregion
  1862. #region Internal Functions
  1863. ;===============================================================================
  1864. ;
  1865. ; Function Name:   __WordGetHWND()
  1866. ; Description:        Returns the hwnd of a word object
  1867. ; Parameter(s):        None
  1868. ; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
  1869. ; Return Value(s):  On Success     - Returns 1
  1870. ;                   On Failure    - None
  1871. ; Author(s):        Bob Anthony
  1872. ;
  1873. ;===============================================================================
  1874. ;
  1875. Func __WordGetHWND(ByRef $o_object)
  1876.     If Not IsObj($o_object) Then
  1877.         SetError($_WordStatus_InvalidDataType, 1)
  1878.         Return 0
  1879.     EndIf
  1880.     
  1881.     If Not __WordIsObjType($o_object, "window") Then
  1882.         SetError($_WordStatus_InvalidObjectType, 1)
  1883.         Return 0
  1884.     EndIf
  1885.     
  1886.     ; Setup internal error handler to Trap COM errors, turn off error notification
  1887.     Local $status = __WordInternalErrorHandlerRegister()
  1888.     If Not $status Then __WordErrorNotify("Warning", "internal function __WordGetHWND", _
  1889.             "Cannot register internal error handler, cannot trap COM errors", _
  1890.             "Use _WordErrorHandlerRegister() to register a user error handler")
  1891.     Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
  1892.     _WordErrorNotify(False)
  1893.     ;
  1894.     Local $s_Title, $h_hwnd
  1895.     
  1896.     $s_Title = ($o_object.Caption & " - " & $o_object.Application.Caption)
  1897.     If Not $oWordErrorHandler.number = 0 Then
  1898.         SetError($_WordStatus_ComError)
  1899.         Return 0
  1900.     EndIf
  1901.     
  1902.     ; restore error notify and error handler status
  1903.     _WordErrorNotify($f_NotifyStatus) ; restore notification status
  1904.     __WordInternalErrorHandlerDeRegister()
  1905.     
  1906.     $h_hwnd = WinGetHandle($s_Title)
  1907.     If @error Then
  1908.         SetError($_WordStatus_GeneralError)
  1909.         Return 0
  1910.     EndIf
  1911.     
  1912.     SetError($_WordStatus_Success)
  1913.     Return $h_hwnd
  1914. EndFunc   ;==>__WordGetHWND
  1915.  
  1916. Func __WordErrorNotify($s_severity, $s_func, $s_status = "", $s_message = "")
  1917.     If $_WordErrorNotify Or $__WordAU3Debug Then
  1918.         Local $sStr = "--> Word.au3 " & $s_severity & " from function " & $s_func
  1919.         If Not $s_status = "" Then $sStr &= ", " & $s_status
  1920.         If Not $s_message = "" Then $sStr &= " (" & $s_message & ")"
  1921.         ConsoleWrite($sStr & @CR)
  1922.     EndIf
  1923.     Return 1
  1924. EndFunc   ;==>__WordErrorNotify
  1925.  
  1926. Func __WordInternalErrorHandlerRegister()
  1927.     Local $sCurrentErrorHandler = ObjEvent("AutoIt.Error")
  1928.     If $sCurrentErrorHandler <> "" And Not IsObj($oWordErrorHandler) Then
  1929.         ; We've got trouble... User COM Error handler assigned without using _WordUserErrorHandlerRegister
  1930.         SetError($_WordStatus_GeneralError)
  1931.         Return 0
  1932.     EndIf
  1933.     $oWordErrorHandler = ""
  1934.     $oWordErrorHandler = ObjEvent("AutoIt.Error", "__WordInternalErrorHandler")
  1935.     If IsObj($oWordErrorHandler) Then
  1936.         SetError($_WordStatus_Success)
  1937.         Return 1
  1938.     Else
  1939.         SetError($_WordStatus_GeneralError)
  1940.         Return 0
  1941.     EndIf
  1942. EndFunc   ;==>__WordInternalErrorHandlerRegister
  1943.  
  1944. Func __WordInternalErrorHandlerDeRegister()
  1945.     $oWordErrorHandler = ""
  1946.     If $sWordUserErrorHandler <> "" Then
  1947.         $oWordErrorHandler = ObjEvent("AutoIt.Error", $sWordUserErrorHandler)
  1948.     EndIf
  1949.     SetError($_WordStatus_Success)
  1950.     Return 1
  1951. EndFunc   ;==>__WordInternalErrorHandlerDeRegister
  1952.  
  1953. Func __WordInternalErrorHandler()
  1954.     $WordComErrorScriptline = $oWordErrorHandler.scriptline
  1955.     $WordComErrorNumber = $oWordErrorHandler.number
  1956.     $WordComErrorNumberHex = Hex($oWordErrorHandler.number, 8)
  1957.     $WordComErrorDescription = StringStripWS($oWordErrorHandler.description, 2)
  1958.     $WordComErrorWinDescription = StringStripWS($oWordErrorHandler.WinDescription, 2)
  1959.     $WordComErrorSource = $oWordErrorHandler.Source
  1960.     $WordComErrorHelpFile = $oWordErrorHandler.HelpFile
  1961.     $WordComErrorHelpContext = $oWordErrorHandler.HelpContext
  1962.     $WordComErrorLastDllError = $oWordErrorHandler.LastDllError
  1963.     $WordComErrorOutput = ""
  1964.     $WordComErrorOutput &= "--> COM Error Encountered in " & @ScriptName & @CR
  1965.     $WordComErrorOutput &= "----> $WordComErrorScriptline = " & $WordComErrorScriptline & @CR
  1966.     $WordComErrorOutput &= "----> $WordComErrorNumberHex = " & $WordComErrorNumberHex & @CR
  1967.     $WordComErrorOutput &= "----> $WordComErrorNumber = " & $WordComErrorNumber & @CR
  1968.     $WordComErrorOutput &= "----> $WordComErrorWinDescription = " & $WordComErrorWinDescription & @CR
  1969.     $WordComErrorOutput &= "----> $WordComErrorDescription = " & $WordComErrorDescription & @CR
  1970.     $WordComErrorOutput &= "----> $WordComErrorSource = " & $WordComErrorSource & @CR
  1971.     $WordComErrorOutput &= "----> $WordComErrorHelpFile = " & $WordComErrorHelpFile & @CR
  1972.     $WordComErrorOutput &= "----> $WordComErrorHelpContext = " & $WordComErrorHelpContext & @CR
  1973.     $WordComErrorOutput &= "----> $WordComErrorLastDllError = " & $WordComErrorLastDllError & @CR
  1974.     If $_WordErrorNotify Or $__WordAU3Debug Then ConsoleWrite($WordComErrorOutput & @CR)
  1975.     SetError($_WordStatus_ComError)
  1976.     Return
  1977. EndFunc   ;==>__WordInternalErrorHandler
  1978.  
  1979. ;===============================================================================
  1980. ;
  1981. ; Function Name:    __WordLockSetForegroundWindow()
  1982. ; Description:        Locks (and Unlocks) current Foreground Window focus to prevent a new window
  1983. ;                    from stealing it (e.g. when creating invisible window)
  1984. ; Parameter(s):        $nLockCode    - 1 Lock Foreground Window Focus, 2 Unlock Foreground Window Focus
  1985. ; Requirement(s):   Windows 2000/Windows ME or higher
  1986. ; Return Value(s):  On Success     - 1
  1987. ;                   On Failure     - 0  and sets @ERROR and @EXTENDED to non-zero values
  1988. ; Author(s):        Valik
  1989. ;
  1990. ;===============================================================================
  1991. ;
  1992. Func __WordLockSetForegroundWindow($nLockCode)
  1993.     Local $aRet = DllCall("user32.dll", "int", "LockSetForegroundWindow", "int", $nLockCode)
  1994.     If @error Then
  1995.         SetError(@error, @extended)
  1996.         Return False
  1997.     EndIf
  1998.     Return $aRet[0]
  1999. EndFunc   ;==>__WordLockSetForegroundWindow
  2000.  
  2001. ;===============================================================================
  2002. ; Function Name:    __WordIsObjType()
  2003. ; Description:        Check to see if an object variable is of a specific type
  2004. ; Author(s):        Bob Anthony (Code based off IE.au3)
  2005. ;===============================================================================
  2006. Func __WordIsObjType(ByRef $o_object, $s_type)
  2007.     If Not IsObj($o_object) Then
  2008.         SetError($_WordStatus_InvalidDataType, 1)
  2009.         Return 0
  2010.     EndIf
  2011.     
  2012.     ; Setup internal error handler to Trap COM errors, turn off error notification
  2013.     Local $status = __WordInternalErrorHandlerRegister()
  2014.     If Not $status Then __WordErrorNotify("Warning", "internal function __WordIsObjType", _
  2015.             "Cannot register internal error handler, cannot trap COM errors", _
  2016.             "Use _WordErrorHandlerRegister() to register a user error handler")
  2017.     Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
  2018.     _WordErrorNotify(False)
  2019.     ;
  2020.     Local $s_Name = ObjName($o_object), $objectOK = False
  2021.     
  2022.     Switch $s_type
  2023.         Case "wordobj"
  2024.             If __WordIsObjType($o_object, "application") Then
  2025.                 $objectOK = True
  2026.             ElseIf __WordIsObjType($o_object, "window") Then
  2027.                 $objectOK = True
  2028.             ElseIf __WordIsObjType($o_object, "document") Then
  2029.                 $objectOK = True
  2030.             EndIf
  2031.         Case "application"
  2032.             If $s_Name = "_Application" Then $objectOK = True
  2033.         Case "document"
  2034.             If $s_Name = "_Document" Then $objectOK = True
  2035.         Case "documents"
  2036.             If $s_Name = "Documents" Then $objectOK = True
  2037.         Case "range"
  2038.             If $s_Name = "Range" Then $objectOK = True
  2039.         Case "window"
  2040.             If $s_Name = "Window" Then $objectOK = True
  2041.         Case "windows"
  2042.             If $s_Name = "Windows" Then $objectOK = True
  2043.         Case Else
  2044.             ; Unsupported ObjType specified
  2045.             SetError($_WordStatus_InvalidValue, 2)
  2046.             Return 0
  2047.     EndSwitch
  2048.     
  2049.     ; restore error notify and error handler status
  2050.     _WordErrorNotify($f_NotifyStatus) ; restore notification status
  2051.     __WordInternalErrorHandlerDeRegister()
  2052.     
  2053.     If $objectOK Then
  2054.         SetError($_WordStatus_Success)
  2055.         Return 1
  2056.     Else
  2057.         SetError($_WordStatus_InvalidObjectType, 1)
  2058.         Return 0
  2059.     EndIf
  2060.     
  2061. EndFunc   ;==>__WordIsObjType
  2062. #endregion